skip to content

When the OS signals memory pressure, what should a city-guide app's in-memory cache release first, and what should it keep?

level: middleimportance: should knowfreq 42%

answer

  1. a request before a kill
  2. what already has a disk copy
  3. who still holds a reference
  4. stop refilling while emptying
  5. a lower ceiling for a while

basics

~20 s

Release every cached entry no visible screen is using, starting with those that have a disk copy, pause prefetching, and lower the tier's ceiling for a while. Keep what visible screens hold; dropping it frees nothing.

solid answer

~40 s

The signal is the OS asking for memory back before it starts ending processes, and the memory tier is the natural donor because nearly everything in it can be reloaded. I'd first **stop refilling**: pause prefetch and cancel pending prefetches. Then release every entry that no visible screen is using; venue pages and search results have disk copies, and derived lists can be recomputed. I'd keep the visible screen's data: the screen still references it, so removing it from the map frees nothing and only causes a miss. Then lower the tier's ceiling for a while so it does not grow straight back, drop to the visible set if the signal repeats, and restore gradually. The disk tier stays untouched; files cost storage, not memory.

go deeper

for a junior

Recall that the OS can ask an app to free memory before it ends processes, and that cached data with a copy on disk is the first thing to give back.

for a middle

Explain the trim order: stop prefetching, release entries no visible screen uses, lower the ceiling, and why the visible screen's data is kept because its references keep it alive anyway.

for a senior

Show the production judgment: proportionate trims instead of clear-and-refill, trimming on going to the background, and checking that screens no longer visible actually release their data.

for a principal

Frame memory pressure as a contract every feature's cache must honour, with one trim entry point and a measured footprint per feature on the lowest supported phone.

When a phone runs short of memory, the OS first reclaims what it can on its own, then asks running apps to release memory, and if that is still not enough it ends processes. Both native platforms work this way in outline. On one platform, the system ends suspended apps before it warns the running ones; on the other, the app is notified that memory is low, and if that does not free enough the system starts ending processes. Why processes die, and how to measure the app's footprint, belongs to the device-budget part of the design. What belongs here is the cache's answer to the request. ## The signal is a request to give memory back A **memory-pressure signal** says: free what you can, quickly. The memory tier is the obvious donor, because by design almost everything in it has a copy somewhere else. One platform's guidance says it directly: release large data that already has an on-disk representation and can be reloaded later. Failing to reduce memory may end in termination, which costs the user far more than a few disk reads. ## What the memory tier can give back | Data in the memory tier | Copy elsewhere | Cost to rebuild | On the signal | |---|---|---|---| | Venue pages not on screen | disk tier | a read and a parse | release | | Search-result pages already left | disk tier or network | a parse or a refetch | release | | Derived lists (sorted, filtered) | recomputable from entries | some CPU | release | | Prefetched venues not yet shown | disk tier, or none | a refetch later | release; cancel pending prefetches | | The venue on the visible screen | disk tier | none needed | keep: the screen holds it anyway | ## A trim policy in steps 1. **Stop adding.** Pause prefetching and warm-up, and cancel pending prefetches, so the tier is not refilled while it is being emptied. 2. **Release everything no visible screen is using.** With a disk copy behind each entry, nothing is lost, only a read on the next visit. 3. **Lower the ceiling for a while**, for example to half, so normal navigation does not grow the tier straight back to the size that caused the signal. 4. **If the signal repeats**, drop to only what visible screens use, and keep the lower ceiling longer. 5. **Restore gradually** once the pressure passes, letting the tier grow back as the user navigates. ## Why clearing the map can free nothing An entry is freed only when nothing references it. If a screen's view model still holds the venue object, removing it from the cache map frees no memory at all: the object lives on, now outside the cache's accounting. That is why the visible screen's data is kept: dropping it from the map buys nothing and makes the next lookup miss. It also means the cache's byte count is a measure of what the cache owns, not of everything the app holds. Releasing memory for real means making sure screens that are no longer visible have let go of their data too. ## Going to the background Trimming the memory tier when the app moves to the background is common practice. The screens are not visible, every entry has a disk copy or can be refetched, and on one platform a suspended app is ended without ever hearing a signal, so waiting for one is not a plan. Keep the disk tier as it is: it is what makes the return fast, and it costs no memory. ## Mistakes interviewers listen for - **Ignoring the signal** in the hope the OS frees memory on its own; the next step after the signal is ending processes. - **Deleting disk cache files** in response; files take storage, not the app's memory, and they are what makes a reload cheap. - **Clearing the map and calling it done** while screens still reference the objects. - **Prefetching through the pressure** to keep the next screens fast, which refills what was just released. - **Reading a signal as proof of a leak**; it reports pressure across the whole device, though a leak can make the app the one that suffers. ## Where this question stops - Why low memory ends processes, and the app's measured footprint, belong to the device-budget discussion. - Decoded images and their own caches are trimmed by the image pipeline under its own budget. - Unsaved user input is not cache; saving it before a possible kill is a lifecycle question, and one platform's guidance does suggest writing unsaved data to disk as part of the cleanup.

  • Should the memory tier shrink to zero, or to a smaller ceiling?
    Respond in proportion. Drop everything no visible screen uses and halve the ceiling for a while, then let it grow back as the user navigates. Clearing to zero and then prefetching recreates the pressure within seconds; leaving the ceiling unchanged lets ordinary navigation refill the tier to the size that caused the signal.
  • Should the app trim its memory tier when it goes to the background?
    That is common practice. Nothing is visible, every entry can be reloaded, and on one platform the system ends suspended apps before it warns running ones, so a backgrounded app may never hear a signal. Keep the disk tier as it is: it costs no memory and makes the return fast.

  • Ignore memory warnings; the OS will free what it needs by itself.
  • Delete the disk cache files to answer a memory-pressure signal.
  • Removing entries from the cache map frees them even while screens hold them.
  • Keep prefetching under memory pressure so the next screens stay fast.
  • A memory-pressure signal proves the app has a memory leak.