In a phone photo-editing app, memory grows about 200 MB each time the editor screen opens and closes until the OS ends it; what keeps a closed screen alive, and how do you tell a leak from a cache?
answer
- something long-lived still points at it
- listeners, tasks, timers, singletons
- a closure that captured the screen
- a static map with no bound
- a cache plateaus, a leak climbs
basics
~20 sSomething long-lived still references the closed screen: a listener never removed, a task or timer that captured it, a singleton or static map holding it. A cache plateaus at its bound; a leak climbs every open-close cycle.
solid answer
~50 sEach editor holds its decoded photo, about 192 MB, so one retained screen leaks a whole image. The classes seen on phones: a **listener** registered with an app-wide object (settings, an event hub, a sensor) and never removed; a **callback** held by a long-running task — a render, an upload, an export — that captured the screen and outlives it; a **repeating timer** or animation tick still firing; a **singleton or static field** holding the screen, its view or its context; a stored **closure** capturing the screen, which in a reference-counted runtime can also form a cycle; and a **static map of bitmaps** with no bound. Repeat open-close cycles and return to an idle screen: a cache levels off at its bound and gives memory back under pressure, while a leak's floor rises a screen's worth per cycle and live editor screens multiply.
code
pseudocode · 13 lineson editor_opened(editor):
editor.scope.add(settings_hub.listen(editor.on_settings_changed))
editor.scope.add(every(1 second, editor.refresh_status))
editor.scope.add(start_background(load_original(editor.photo_id)))
on editor_closed(editor):
for handle in editor.scope:
handle.cancel() # listener removed, timer stopped, task cancelled
editor.full_image = none # the decoded pixels leave with the screen
# the load reports to a long-lived owner, never to the screen, so no late result can reach a closed screen
on load_finished(photo_id, pixels):
image_cache.put_bounded(photo_id, pixels)go deeper
Recall that a closed screen stays in memory when something long-lived still references it, such as a listener that was never removed.
Explain the main classes — listeners, tasks, timers, singletons, closures, static bitmap maps — and how tying each to the screen's lifetime prevents them.
Show the repeat-cycle test that separates a leak from a bounded cache, and how to spot leaks in the field from kills that grow with session length.
Set the rules every team follows — lifetime-scoped registrations, no screens in globals, leak checks in tests — and decide how leak regressions block a release.
## Why a leaked screen is so expensive on a phone A **leak**, in a managed runtime, is memory that stays reachable after the program no longer needs it. On a server it often grows slowly. In a photo editor it arrives in large steps: each editor screen holds the decoded photo (48 million pixels x 4 bytes, about 192 MB), plus previews and layer data. If the closed screen stays reachable, all of that stays too. Three or four open-close cycles can push the app past what the phone will let it hold, and the OS ends it — in the foreground, where the user sees it vanish. ## Leak classes on a phone | Class | How it keeps the closed screen alive | Fix | |---|---|---| | Listener on an app-wide object | the settings store, event hub or sensor keeps a reference to the screen's handler | unregister when the screen closes | | Callback held by a long task | an export, upload or render job captured the screen and runs on after it closed | cancel the task with the screen; deliver results to a long-lived owner, not the screen | | Repeating timer or animation tick | the timer's handler references the screen and the timer is never stopped | stop timers in the close callback | | Singleton or static field | a global "current editor", view or context reference | hold an id, not the screen; clear it on close | | Stored closure | a closure saved in a long-lived place captures the screen | capture only what is needed, or capture weakly | | Static map of bitmaps | a "cache" keyed by photo id with no bound or eviction | a bounded cache that sheds under pressure | ## Two runtime flavours, one rule Mobile stacks use both kinds of automatic memory management: - **Tracing collectors** free anything not reachable from a root, including cycles. A leak there is always a path from something long-lived — a static, a singleton, a running thread, a registered listener — to the screen. - **Reference counting** frees an object when no strong reference remains. Here a cycle leaks too: the screen stores a closure, and the closure strongly captures the screen. Neither count reaches zero. The rule for both: **anything that outlives the screen must not hold a strong reference to it.** ## Leak or cache? Both make memory go up. The test separates them: 1. Open the editor, close it and return to an idle screen; repeat ten times. 2. Give the runtime a chance to reclaim memory, then read the app's memory floor after each cycle. 3. A **cache** levels off at its configured bound, and when the OS signals pressure or the cache is trimmed, memory comes back down. 4. A **leak** raises the floor by roughly one screen's worth every cycle, and never gives it back. 5. Confirm by counting live editor screens after the cycles. The answer should be zero, or one if the design keeps one warm. Ten means a leak; the reference path from a long-lived root names its class. A large but bounded cache is a sizing decision. Growth that tracks the number of times a screen was opened is a leak. ## Design rules that prevent the classes - Tie every registration, task and timer to the screen's lifetime: what the screen starts, the screen's close stops. - Let long-running work report to a long-lived owner — a repository or a job manager — rather than to the screen that started it. - Keep decoded bitmaps only in bounded caches with eviction, never in static maps. - Store ids, not screens, in anything global. - In the editor's own tests, open and close the screen repeatedly and assert that no editor instance survives. Several platforms and frameworks offer debug-build leak detectors that automate exactly this check. ## Seeing it in the field Leaks show up as kills that correlate with session length or with how often a screen was opened: a rising memory floor through the session, and memory-related terminations concentrated in long sessions. Reading those per version catches the release that introduced a new listener without a matching removal.
- Why is a closure that captures the editor screen a particular risk in a reference-counted runtime?If the screen stores the closure and the closure strongly captures the screen, each keeps the other's count above zero, so neither is freed after the screen closes. A tracing collector would reclaim that cycle; reference counting needs the capture to be weak or the closure to be released on close.
- The leak never reproduces on your development phone. How would it show up in the field?As memory kills that rise with session length and with how often the editor was opened, concentrated on lower-memory devices. Read peak memory and memory-kill rates per version and device class, and look for the release in which they climbed; then reproduce with repeated open-close cycles on a low-memory phone.
- A garbage-collected runtime cannot leak memory
- Any cache that grows during a session is a leak
- Unregistering a listener when the screen closes is optional, since the screen is gone
- A background task that outlives its screen cannot keep the screen in memory
- A static map of decoded bitmaps keyed by photo id is a safe app-wide cache