skip to content

Why does the OS end a phone photo editor while its user is in the camera, and how should the editor avoid losing the edit?

level: middleimportance: must knowfreq 55%

answer

  1. the camera is hungry too
  2. background apps go first
  3. a kill runs none of your code
  4. a warning is a courtesy, not a contract
  5. save operations, drop rebuildable pixels

basics

~20 s

The camera creates memory pressure, and the OS reclaims background apps first, a large editor early, without running its code. Save each edit's operations as it commits, and drop rebuildable pixels when leaving the screen.

solid answer

~50 s

When the user switches to the camera, the editor becomes a background app while a memory-hungry app takes the foreground. Under pressure the OS reclaims **background processes first**; one platform's guidance says it kills the most memory-intensive apps when memory runs short, so an editor holding a 192 MB decoded photo is an early candidate. The kill runs none of the app's code. A warning may come first, but not reliably: one platform deprecated most of its memory-pressure callbacks because they did not prevent kills. So the editor **writes each edit's operations to disk as the edit is committed**, not at the warning; on leaving the screen it **releases everything it can rebuild** — decoded full-resolution buffers, previews, render tiles — keeping only the small operation list; and it counts these kills in the field. A small footprint lowers the odds; it guarantees nothing.

code

json · 11 lines
json
{
  "editId": "7c2e4b1a-93d5-4f60-8a1e-2b9c7d4e5f61",
  "source": { "photoRef": "library-item-8812", "width": 8000, "height": 6000 },
  "operations": [
    { "seq": 1, "op": "crop", "rect": [400, 300, 7200, 5400] },
    { "seq": 2, "op": "exposure", "value": 0.35 },
    { "seq": 3, "op": "mask-brush", "layer": 2, "strokes": "strokes/7c2e-3.bin" }
  ],
  "checkpoint": { "afterSeq": 0, "file": "checkpoints/7c2e-0.bin" },
  "savedAt": "2026-10-07T09:14:52Z"
}

go deeper

for a junior

Recall that the OS ends background apps to free memory for the one in front, often without a warning and without running your code, so unsaved work in memory is lost.

for a middle

Explain the editor's two moves: write operations to disk on every commit, and release decoded pixels and previews when the app leaves the screen.

for a senior

Show that you distinguish foreground from background kills, never depend on warnings, and measure memory kills per version and device class in the field.

for a principal

Weigh how much rebuild time on return is acceptable against the background footprint, and set that rule for every memory-heavy screen in the app.

## What happens when the user opens the camera 1. The user is halfway through an edit; the editor holds the decoded photo (about 192 MB at four bytes a pixel for 48 megapixels), previews and layer data. 2. They switch to the camera. The editor moves to the background; the camera, itself memory-hungry, takes the foreground. 3. Free memory runs low. The OS first reclaims what it can without ending anything — on one platform by compressing memory into a swap area in RAM, on the other possibly by moving a memory-heavy background app's memory to storage. Both make the eventual return slower. 4. If that is not enough, the OS **ends processes**, background ones first. 5. The user comes back to an app that launches from scratch. If the edit lived only in memory, it is gone. ## How the OS chooses - **Background before foreground.** Both platforms reclaim background apps before the one on screen. One ranks processes by importance categories: background apps first, the most recently used "previous" app a little later, the foreground app near the end and system processes last. - **Big before small, on one platform.** Its guidance says that when memory becomes limited it kills the most memory-intensive apps. A large editor is an early target even when it was used a minute ago. - **The foreground is not safe.** An app that grows past what the device can spare while on screen is ended too; the user sees it vanish, which looks like a crash. - **No code runs at the kill.** One platform states that the usual teardown callback is not guaranteed when the process is killed. Whatever was not on disk before the kill is lost. ## Warnings are a courtesy, not a contract One platform sends a low-memory warning to running apps when it cannot reclaim enough by ending suspended ones; failing to shed memory may lead to termination, and its guidance suggests writing unsaved data then. The other platform has deprecated nearly all of its memory-pressure callbacks, saying they had not helped prevent low-memory kills, and keeps only the signals that the UI was hidden or the app went to the background. A suspended app may never run again before it is ended. The design therefore never depends on a warning to save work. ## The editor's design | State | Where it lives | When the editor leaves the screen | |---|---|---| | Edit operations (crop, exposure, brush strokes) | memory, mirrored to disk on every commit | already on disk; keep the small list | | Reference to the source photo | disk (it is the user's original) | nothing to do | | Decoded full-resolution pixels | memory | release; rebuild from source and operations | | Previews and render tiles | memory | release | | Undo checkpoints | disk | nothing to do | The rules behind the table: - **Save on commit, not on warning.** Each completed edit appends a record of a few hundred bytes. That write is cheap enough to do every time, and it is the only save that survives a foreground kill or a crash. - **Shrink on leaving the screen.** The move to the background is the best moment to drop rebuildable pixels: the app still runs when it is told, and both platforms advise freeing memory then. A 20 MB background footprint instead of 400 MB lowers the chance of being chosen; it is not a guarantee. - **Rebuild cost is the price.** Returning means decoding the source and replaying operations, a second or two on a mid-range phone (an assumption to measure). That is far better than losing the edit. ## Measuring it in the field - One platform reports, on the next launch, why the previous process ended, including low memory; record that reason with the app version and device class. - The other platform reports peak memory and memory at suspension per release and device, and produces a diagnostic report when an app approaches its memory limit. - Track the share of sessions ending in a memory kill, foreground and background separately, and compare releases. Getting the user back to the same screen, scroll position and tool after such a kill is a lifecycle question; this design guarantees there is something to restore.

  • Does keeping the editor's background footprint small guarantee it survives a trip to the camera?
    No. The OS can end any background app when it needs memory; a small footprint only makes the editor a later or less attractive candidate. That is why saving operations on every commit is the guarantee and shrinking is the optimisation.
  • How would you know how often users lose edits this way?
    Record, on the next launch, why the previous process ended where the platform reports it, and on the other platform read peak memory, memory at suspension and the reports issued near the limit. Count memory kills per session by version and device class, and separately how often a relaunch found a saved but unfinished edit.

  • The OS always sends a memory warning before it ends a background app
  • A foreground app is never ended for using too much memory
  • Saving the edit when the app goes to the background protects it from every kill
  • An app that restarted after a trip to the camera must have crashed
  • The OS saves an app's memory to disk before ending it, so nothing is lost