Skip to main content
Be Bitwise

Memory Management Across Platforms

Mobile vs Desktop Comparison · Mobile Internals track

7 min read

In this module

The Swap Question

One of the starkest mobile-versus-desktop divergences is how each handles memory pressure. Desktop systems have leaned on disk-backed swap for decades. RAM fills up, the kernel parks inactive pages on a swap partition, life carries on. Mobile devices take a wholly different route, shaped by the realities of flash storage wear and battery life.

Desktop: Traditional Swap

On a desktop Linux or Windows system, the kernel keeps a swap partition (or swap file) on the storage device. When physical RAM runs dry, the kernel picks pages that haven't been touched recently and writes them out to swap, freeing RAM for active processes. When a swapped page is needed again, the kernel reads it back from disk via a page fault.

This approach works well on desktops because:

  • Storage devices (SSDs, HDDs) are large and relatively inexpensive.
  • Desktop workloads often involve many idle applications that benefit from being swapped out.
  • The performance penalty of swapping (especially on SSDs) is manageable.

iOS: Compressed Memory and Jetsam

iPhones have no swap to persistent storage. (The exception is iPadOS: since iPadOS 16, M-series iPads can swap to storage as "virtual memory swap".) Flash memory on iOS devices has finite write endurance, and constant swap I/O would chew through the storage and bleed the battery dry. So iOS reaches for two other tools.

The Compressor

When memory pressure rises, the XNU kernel's VM compressor squeezes inactive pages in place rather than writing them out to disk. Compression shrinks the footprint of idle pages while keeping them accessible without any I/O. Decompression is fast enough that the latency is laughable compared to actually hitting disk.

Jetsam

When compression isn't enough and memory pressure tips into critical territory, jetsam (the kernel's out-of-memory killer) starts terminating background applications to free RAM. Jetsam picks victims based on memory footprint and priority level. This is why iOS apps have to be ready to be killed at any moment. There's no guarantee a backgrounded app will survive the next memory squeeze.

iOS Memory: No Swap

iOS on iPhone does not use disk-backed swap (M-series iPads are the exception). Instead, the kernel compresses inactive pages in RAM (reducing their footprint without I/O) and terminates background apps via jetsam when memory pressure is critical. Apps must handle sudden termination gracefully. This design preserves flash storage lifespan and battery.

Android: ZRAM

Android splits the difference with ZRAM (compressed RAM). ZRAM creates a compressed block device in RAM that acts as a swap target. When the kernel wants to swap pages out, it compresses them into ZRAM rather than punching them through to flash.

bash
# ZRAM configuration on Android
$ cat /proc/swaps
Filename Type Size Used Priority
/dev/block/zram0 partition 2097148 524288 5
# Swap exists, but it is backed by compressed RAM, not disk

When reclaim and ZRAM can't keep up, Android's lmkd (Low Memory Killer Daemon), which replaced the earlier kernel-based lowmemorykiller, starts terminating processes based on their oom_score_adj value. Processes with higher scores (less important) get killed first: cached apps around 900 and up, the foreground app at 0. Since Android 10, lmkd decides when to act by watching the kernel's PSI (pressure stall information), which measures how long tasks sit stalled waiting for memory, so it can start killing before ZRAM is completely full.

Memory pressure on a phone

Raise the demand or open more apps, then switch platform to compare how each one finds the room. All sizes are illustrative.

  1. 1. Fits in RAM
  2. 2. Drop clean and purgeable pages
  3. 3. Memory compressor
  4. 4. Memory warnings
  5. 5. Jetsam

Fits in RAM. Everything fits, with RAM to spare.

  • Kernel and wired 800 MB
  • File cache and purgeable 400 MB
  • Processes, uncompressed 2,750 MB
  • Compressor 0 MB
  • Free 146 MB
Processes, in the order this platform would kill them
Kill orderProcessPriorityIn RAMCompressed
1NotesSuspended250 MB0 MB
2GameSuspended600 MB0 MB
3BrowserSuspended500 MB0 MB
4Chat (syncing)Background (running)200 MB0 MB
5Maps (navigating)Background (running)200 MB0 MB
6CameraForeground500 MB0 MB
neverSystem servicesSystem daemon500 MB0 MB

Compression ratio assumed 2:1 and the compressor capped at 1,024 MB of original pages; real values vary with the data and the device. Jetsam's real priority bands are finer than the three groups shown, and it also kills any app that goes over its own memory limit. RAM 4,096 MB, all sizes illustrative.

Drag the demand slider slowly and watch each platform work down its list of stages. Then press Open Photo editor on Android and check where the Camera app lands in the kill order now that it's no longer in front, and try the same demand on desktop Linux, where the OOM killer goes after the biggest process instead.

Object Lifetime: ARC vs Garbage Collection

Beyond system-level memory management, the two platforms also part ways at the language level on how application code manages object lifetimes.

iOS: Automatic Reference Counting (ARC)

iOS uses ARC (Automatic Reference Counting), a compile-time mechanism. The compiler injects retain and release calls based on static analysis of object ownership. The instant an object's reference count drops to zero, it's deallocated.

objc
// ARC inserts retain/release at compile time
- (void)example {
NSString *str = [[NSString alloc] initWithString:@"hello"];
// Compiler inserts: [str retain] (implicit from alloc)
[self useString:str];
// Compiler inserts: [str release] at end of scope
// If refcount reaches 0, memory is freed immediately
}

ARC gives you deterministic deallocation. Objects are freed at predictable points in the code, no garbage collection pauses. The catch is retain cycles: two objects holding references to each other, neither's count ever reaching zero. The programmer has to break the cycle with a weak reference.

Android: Garbage Collection

Android's ART runtime ships a tracing garbage collector. The GC periodically walks the object graph, picks out objects that are no longer reachable from any root reference, and reclaims their memory.

java
// Garbage collection determines when objects are freed
public void example() {
String str = new String("hello");
useString(str);
// str becomes eligible for GC when no references remain
// Actual deallocation happens at GC's discretion
}

ART's garbage collector has come a long way. Modern versions ship a concurrent copying collector that compacts the heap while the application keeps running, slashing pause times into the millisecond range. GC still involves periodic work, though, and that work can show up as latency you didn't expect.

ARC vs Garbage Collection

iOS uses ARC (Automatic Reference Counting): the compiler inserts retain/release calls, and objects are freed immediately when their reference count reaches zero. Android uses a tracing garbage collector in ART that periodically identifies and reclaims unreachable objects. ARC is deterministic but requires careful handling of retain cycles. GC is automatic but introduces periodic pause latency.

Security Implications

These memory management decisions have direct security consequences:

  • No swap to disk (iPhone) means sensitive data in memory doesn't land on persistent storage through paging where a forensic examiner could pull it out later. On desktop systems with disk swap, memory contents can hang around on disk long after the process is gone.
  • ZRAM compression (Android) keeps swap data in RAM, which is volatile and disappears on reboot or power loss. Better than disk swap from a forensic angle, less ideal than no swap at all. Some vendors add storage-backed swap on top (Samsung's RAM Plus, or zram writeback), which brings the disk problem back.
  • Jetsam/lmkd termination means exploit payloads in backgrounded processes may be killed before the attacker can pivot, putting the attacker on a clock that doesn't exist on desktop.
  • ARC's deterministic deallocation makes use-after-free bugs in Objective-C more predictable (you know exactly when objects are freed), which cuts both ways for an exploit author.
  • GC non-determinism on Android makes managed-object lifetimes less predictable, which complicates some heap-grooming tricks in Java/Kotlin code. It doesn't help much in practice, though: most Android memory-corruption exploits target native code (media parsers, drivers, the GPU stack), where the GC isn't involved at all.

Across the board, mobile memory management is more aggressive and more constrained than desktop. That same aggression that helps battery life also makes both exploitation and forensic recovery a much steeper hill.