📚 Encyclopedia

Android · updated 2026-11-02

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

  1. Per-row ContentProvider IPC before the first frame. The list ViewModel resolved contact names with one ContactsContract.PhoneLookup query per recipient for every row, because sync never stored the title or photo. The cache was in memory and positive-only, and onResume wiped it. Fix: persist resolution in the database (with a contactResolvedAt stamp, negatives included), backfill off the main thread, and re-resolve on a ContactsContract ContentObserver, on new threads, and when READ_CONTACTS is granted. The list then renders straight from the database.
  2. Expensive key derivation per database connection (SQLCipher passphrase mode). See SQLCipher on Android (keys, KDF cost, rekeying safely).
  3. Main-thread init: WorkManager.getInstance() opens WorkManager's own Room database; a Keystore unwrap ran inside Hilt's provideDatabase, which executes on the main thread when the first ViewModel is built. Move enqueues to an app-scope coroutine and open the database lazily.
  4. "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

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

See also

Linked from

Sources: message memory (startup_perf_2026_09), tldr post #1668, tldr post #1874