iOS 12 App State Preservation Under Memory Pressure
In iOS 12, maintaining a seamless user experience during sudden memory pressure requires a coordinated effort between the operating system's kernel and UIKit's state restoration architecture. When physical memory thresholds are breached, the system initiates aggressive memory reclamation protocols, demanding that applications rapidly purge nonessential data or face termination. Understanding how iOS 12 signals memory strain, invokes the Jetsam daemon, and utilizes disk-based state archives allows developers to ensure user context remains intact even after an app is abruptly killed in the background.
When memory becomes constrained, iOS 12 first attempts to resolve the
issue without terminating apps by dispatching system-level memory
warnings. The operating system notifies the foreground and active
background apps through
UIApplicationDidReceiveMemoryWarningNotification and calls
the applicationDidReceiveMemoryWarning(_:) delegate method,
as well as didReceiveMemoryWarning() on active
UIViewController instances. At this stage, apps are
expected to purge decached images, discard temporary caches, and release
objects that can be easily recreated.
If purging volatile memory is insufficient, or if a memory spike is
exceptionally sudden, the iOS 12 kernel mechanism known as Jetsam steps
in. Jetsam prioritizes processes based on a strict hierarchy: suspended
apps and background tasks with high memory footprints are killed first,
followed by larger foreground backgrounded processes. Because Jetsam
terminates apps abruptly using a SIGKILL signal, the app
receives no further execution time to perform emergency saves.
To withstand this sudden termination, iOS 12 relies on UIKit's opt-in
State Restoration framework. When configured, the app encodes its view
controller hierarchy and critical UI state to disk whenever the
application moves from the foreground to the background via
application(_:shouldSaveApplicationState:). View
controllers implement encodeRestorableState(with:) to
persist essential details such as active scroll positions, unsaved input
text, and navigation paths into restoration archives stored in the local
sandbox.
Because memory pressure can cause termination without warning,
developers in iOS 12 must commit critical state changes incrementally
rather than deferring saves to termination callbacks like
applicationWillTerminate(_:). Data must be persistently
saved to storage using Core Data, SQLite, or file serializations while
the user interacts with the app, or at the moment the app enters the
background via applicationDidEnterBackground(_:).
When the user returns to an app that was terminated by Jetsam, iOS 12
executes a standard launch sequence but consults the saved restoration
archive. If application(_:shouldRestoreApplicationState:)
returns true, UIKit reconstitutes the view controller hierarchy, decodes
the saved state, and presents the user with the exact screen and
interface state that was active prior to termination, effectively
disguising the sudden memory-related kill.