Android app cold-start performance and process death
androidperformancestartupcontactsworkmanagerprocess-death
"Every time I open the app it takes several seconds to load, even after a second away." Diagnosing that in the Message SMS app found four independent causes. The general checklist: nothing slow before the first frame, nothing recomputed that could be persisted, and know whether "reload" means the process died.
Causes found
- Per-row ContentProvider IPC before the first frame. The list ViewModel resolved contact names with one
ContactsContract.PhoneLookupquery per recipient for every row, because sync never stored the title or photo. The cache was in memory and positive-only, andonResumewiped it. Fix: persist resolution in the database (with acontactResolvedAtstamp, negatives included), backfill off the main thread, and re-resolve on aContactsContractContentObserver, on new threads, and when READ_CONTACTS is granted. The list then renders straight from the database. - Expensive key derivation per database connection (SQLCipher passphrase mode). See SQLCipher on Android (keys, KDF cost, rekeying safely).
- Main-thread init:
WorkManager.getInstance()opens WorkManager's own Room database; a Keystore unwrap ran inside Hilt'sprovideDatabase, which executes on the main thread when the first ViewModel is built. Move enqueues to an app-scope coroutine and open the database lazily. - "Reloads after 1 s in the background" = the process was killed (OEM task killer, low memory, or "Don't keep activities"). Nothing on the resume path rebuilds state; a new process does.
Proving process death
- Log
ActivityManager.getHistoricalProcessExitReasons()(API 30+) once per exit at startup.REASON_LOW_MEMORY, orREASON_OTHERwith OEM text, means a killer;REASON_USER_REQUESTEDmeans swiped away. - Without app code:
adb shell dumpsys activity exit-info <package>. - If an OEM killer is the cause, the remaining levers are battery-optimization exemption and the OEM's autostart settings, not code.
Making a persisted-name resolver robust
The persisted contact names (cause 1) needed several fixes found in review before the re-land shipped (v2.34.0, 2026-10-29):
- Make resolution state part of the row. A contactResolvedAt column (0 = never) that every row-construction path starts at 0 lets the resolver find new work through one observed query, without any writer knowing it exists. Anything that changes what the name derives from (for example, a group thread healed from a one-member row) must reset it to 0.
- One writer for derived columns, column-targeted, in a transaction per chunk. Chunks of about 200 rows keep a backfill of thousands to a handful of list re-queries (25 caused 80+).
- LruCache.put(key, null) throws NullPointerException. A "negative cache" that stores null for non-contacts silently never worked (the exception was swallowed) and re-queried every miss. Store a sentinel object instead.
- A permission granted from system Settings kills the process (REASON_PERMISSION_CHANGE), so an "on resume, check whether READ_CONTACTS became granted" hook never sees the edge. Persist the last-seen permission state and compare at the next process start.
- An observer registered synchronously but collected from a coroutine launched later drops events in the gap; give that SharedFlow replay = 1.
- Serialize overlapping refreshes with a Mutex, or one refresh wipes the cache the other is filling.
- A ContentObserver on the Contacts authority, debounced about 2 s, replaces "wipe the cache on every resume".
Gotchas
- Persisting derived data adds a migration; ship it separately from risky changes so a rollback does not have to carry it. (The first attempt bundled a key-format migration and five UI commits, and a crash forced reverting all of it.)
- Measure cold open on device before and after; on the emulator the IME-window-on-screen time is a usable proxy for a keyboard app.
See also
- SQLCipher on Android (keys, KDF cost, rekeying safely)
- Doze, WorkManager backoff, and background sync that stalls