How a Feed App Survives: Two-Level Caching, Weak-Network Degradation, and OOM Defense
Feed apps that ignore peak-memory coordination still crash under heavy scrolling even with a cache in place. The combination of cost-based LRU eviction, RunLoop-gated decoding, and aggressive cancellation on memory warning prevents the transient bitmap spikes that trigger iOS jetsam events, and the instrumentation makes those spikes visible for the first time.
Feed performance collapses when peak resource usage isn't controlled. This implementation layers a SQLite L1 for instant cold-start content, then overlays a two-level image cache: a cost-evicting LRU for decoded bitmaps and URLCache for raw response data. Decoding is deferred to RunLoop idle observers so scrolling never drops frames, and in-flight request merging eliminates duplicate network calls.
Weak-network handling goes beyond a toast alert. When connectivity drops, the system suppresses refreshes and pagination while continuing to serve cached posts, keeping the UI usable without pointless retries. The OOM strategy targets instantaneous memory spikes, not just total cache size, by canceling all in-flight loads, draining the idle-work queue, and disabling prefetch the moment a memory warning fires.
Every decision is instrumented: cache-hit rates, image-load failures, and memory-warning snapshots feed an observability loop that turns OOM debugging from guesswork into data-driven investigation. The result is a feed that loads fast, scrolls smoothly, degrades gracefully, and stops bleeding before the system kills it.
Most feed OOMs are not caused by total memory pressure but by a brief, synchronized spike when scrolling stops and a backlog of decodes all complete at once.
Delaying image decode until the RunLoop is idle is a deliberate trade-off that prioritizes frame rate over immediate image display, and users rarely notice the sub-second delay.
Clearing the image cache on memory warning is insufficient if in-flight downloads and queued decodes are not also cancelled; memory rebounds within seconds otherwise.
Request deduplication is as much a memory optimization as a network one—it prevents duplicate decoded bitmaps from entering the LRU simultaneously.
SQLite as a feed-data L1 turns offline mode from an error state into a normal, usable condition, which changes how weak-network logic is designed.
Observability events tied to cache hits and memory warnings turn OOM debugging from a crash-log guessing game into a measurable pipeline problem.