A memory warning on iOS is the notification the operating system sends to a running app when available physical memory is running low, asking the app to release memory it can rebuild later. In mobile performance, it is the last cooperative signal before the system starts terminating apps to reclaim memory, and UIKit delivers it through didReceiveMemoryWarning, applicationDidReceiveMemoryWarning(_:) and the UIApplication.didReceiveMemoryWarningNotification notification.
Handling memory warnings correctly lowers the chance that an app is killed in the foreground or evicted in the background. Ignoring them does not cause an immediate crash, but it makes the app a more likely target when the system has to choose what to terminate.
How iOS memory pressure leads to a memory warning
iOS raises a memory warning because it does not swap app memory to disk in the way desktop systems do. When physical RAM fills up, the kernel can only discard clean pages, compress dirty pages with the memory compressor, or terminate processes, so it relies on apps to give memory back voluntarily.
The kernel tracks memory pressure levels and a subsystem known as Jetsam decides which processes to terminate when pressure becomes critical. Before that point, the system posts a memory warning to running apps. The order of termination generally follows process priority: suspended background apps go first, and the foreground app is terminated only when it alone exceeds its limit or pressure is extreme.
Each app also has a per-process memory limit that depends on the device model and its RAM. Exceeding it leads to termination even if the rest of the system has free memory. Because these limits are not documented as fixed numbers, the safest approach is to measure with os_proc_available_memory() and Xcode Instruments rather than hard-coding thresholds.
| Stage | System behavior | Effect on the app |
|---|---|---|
| Normal | Free and reclaimable pages are sufficient | No action needed |
| Warning | Compressor busy, memory warning posted | App should release caches |
| Critical | Jetsam terminates low-priority processes | Background apps are evicted |
| Limit exceeded | Process passes its own memory ceiling | App is terminated, even in foreground |
Where an iOS app receives the memory warning
An iOS app can receive the memory warning through several equivalent entry points, and the right one depends on which object owns the memory. The warning is delivered on the main thread, so handlers must be fast and must not allocate large amounts of memory themselves.
| Entry point | Scope | Typical use |
|---|---|---|
| didReceiveMemoryWarning | UIViewController | Release view-specific caches and data |
| applicationDidReceiveMemoryWarning(_:) | App delegate | Purge app-wide caches |
| didReceiveMemoryWarningNotification | Any observer | Decoupled services such as image loaders |
| DispatchSource memory pressure | Process-wide, GCD | Graded reaction to warning and critical levels |
| SwiftUI scene or environment observers | SwiftUI apps | Usually via the notification above |
Older UIKit versions automatically unloaded hidden view hierarchies on a memory warning, but this behavior was removed in iOS 6. Modern code must therefore decide explicitly what to free, and view controllers should never set their view to nil in response to a warning.
What to release and what to keep
A good memory warning handler releases memory that is expensive to hold but cheap to rebuild. Decoded images, prerendered thumbnails, parsed responses and offscreen view snapshots are typical candidates, because they can be regenerated from disk or the network when needed.
Foundation offers NSCache, which evicts entries automatically under memory pressure and is thread-safe, so many apps need little custom handling if their caches are built on it. A plain dictionary of images, by contrast, keeps growing until the app is killed unless it is cleared manually.
| Memory type | Recommended action | Reason |
|---|---|---|
| Decoded image caches | Clear completely | Largest and easy to redecode |
| Prefetched list content | Drop items outside the visible range | Rebuilt on scroll |
| Audio and video buffers | Reduce to the current item | Large dirty memory footprint |
| User-entered unsaved data | Keep and persist to disk | Losing it harms the user |
| Visible UI state | Keep | Needed for the current screen |
Diagnosing memory warnings and terminations
Memory warnings are diagnosed by correlating the warning with the app footprint at the time it arrived. Xcode Instruments offers the Allocations, Leaks and VM Tracker templates, and the Xcode memory gauge shows the live footprint during debugging, which is the number that counts against the limit.
- Xcode Instruments reveals growth patterns, abandoned memory and leaks.
- MetricKit reports memory metrics and exit reasons from real devices, including terminations due to memory limits.
- Jetsam event reports in device analytics show which processes were killed and their page counts.
- Simulate Memory Warning in the iOS Simulator triggers the handlers on demand for testing.
A frequent pattern in crash analytics is a foreground termination without a crash log. Users experience it as a crash, but no exception is recorded, because the process was killed by the system. MetricKit exit metrics and out-of-memory heuristics in analytics tools are the main ways to detect these silent failures.
Common mistakes and misconceptions
The most common mistake is treating the memory warning as the primary memory strategy. By the time it arrives, pressure is already high, and an app with a steadily growing footprint will be terminated regardless of how its handler behaves.
- Assuming the warning always arrives: a sudden large allocation can exceed the limit before any warning is delivered.
- Allocating inside the handler: logging large payloads or rebuilding structures adds pressure at the worst moment.
- Ignoring graphics memory: textures and decoded images often dominate the footprint on iPhone and iPad.
- Confusing it with Low Power Mode: Low Power Mode limits energy use and is unrelated to memory pressure.
- Relying on swap: iOS compresses memory but does not page app data to storage.
The simple explanation
An iPhone has a fixed amount of working memory that all open apps share. When that memory gets nearly full, iOS sends each app a memory warning, which is a polite request to tidy up and give some memory back.
A well-behaved app throws away things it can easily get again, such as pictures it has already shown. An app that ignores the request is like a guest who keeps taking chairs at a full party. Eventually the host asks it to leave. On the phone, that means the app is closed without notice.
How it is used in practice
A team maintains a photo-sharing app for iOS whose users scroll through long feeds of high-resolution images. MetricKit reports show that many sessions on older iPhones end with memory-limit terminations in the foreground, which users report as random crashes.
Profiling with Xcode Instruments reveals a custom dictionary cache holding several hundred megabytes of decoded images. The team replaces it with NSCache with a cost limit, downsamples images to the size displayed on screen, and clears all offscreen images in the memory warning handler. After release, the footprint during scrolling falls by roughly half and memory-related terminations drop sharply, while scrolling remains smooth because visible images are kept.