The App That Ate 2.3 GB of RAM: A Mobile Memory Leak Autopsy

Archi4 min read

androidmobiledebugging

Contents
  1. The crash report pointed at the wrong screen
  2. What made it hard to see
  3. Three technical decisions that turned a bug into an outage
  4. 1. Non-static inner classes inside long-lived objects
  5. 2. A cache with no eviction policy
  6. 3. Bitmaps stored at full sensor resolution
  7. Why the crash-free number moved after each fix
  8. The monitoring gap
  9. What the postmortem changed in the release process
  10. The uncomfortable part

The crash report pointed at the wrong screen#

The app had shipped a new camera filter. Within a week, crash-free sessions on the Samsung Galaxy A54 fell from 99.4 percent to 96.1 percent. Support tickets mentioned the editor, so the team rewrote the editor. The crashes kept coming.

What the crash logs actually showed was an OutOfMemoryError during ordinary feed scrolling, often several screens away from the camera. A mid-range phone with 4 GB of RAM was dying while displaying thumbnails. That gap between symptom and cause is the lesson worth keeping.

The root cause was a retained Activity. A helper object with application-level lifetime stored a reference to the camera screen so it could "callback later." That screen held a 12-megapixel bitmap, a surface texture, and a fragment manager. The garbage collector could never free any of it. Every time the user opened the editor, another Activity leaked.

What made it hard to see#

The leak was slow enough to hide in manual testing. A developer with a flagship phone and 12 GB of RAM could open the editor 30 times and notice nothing. The same person on the target device crashed after six or seven visits, usually while doing something else.

Three technical decisions that turned a bug into an outage#

1. Non-static inner classes inside long-lived objects#

The suspect was an anonymous Handler inside the filter module. In Java and Kotlin, an inner class holds an implicit reference to its enclosing instance. Because the handler was posted to a static queue and the module lived in the application scope, the entire screen stayed alive behind it.

The fix was structural, not cosmetic: a static handler with a WeakReference, and cancellation of pending messages in onDestroy. Teams forget that cancelling a runnable also means removing callbacks from views.

2. A cache with no eviction policy#

The filter preview kept decoded bitmaps in a HashMap keyed by photo ID. Nobody had added a size limit. On a device with plenty of memory this looks like a speed win. On a 4 GB phone already running the launcher, messaging, and a browser, it is a countdown.

Replacing it with an LruCache sized to a fraction of available heap cut resident memory by roughly 60 percent in the editor. The measured figure came from Android Studio's memory profiler across three devices, not from a vendor benchmark.

3. Bitmaps stored at full sensor resolution#

Every preview was decoded at 4000 by 3000 pixels. The screen could show 1080 by 2400. In-sample decoding at the target size reduced each bitmap from about 36 MB to under 4 MB. That single change removed most of the pressure that made the leak fatal.

Why the crash-free number moved after each fix#

Instrumentation told a clearer story than the crash tracker. Heap dumps taken with LeakCanary showed one retained camera Activity after two editor visits on a test build. After the handler change, retained Activities dropped to zero, but memory still climbed during scrolling.

After the cache change, peak heap on the Galaxy A54 settled near 310 MB, down from more than 700 MB during a five-minute session. The remaining growth came from bitmap decoding, which the third change addressed.

Crash-free sessions recovered over two releases. The first release brought 97.8 percent, the second 99.2 percent. Some of that came from the fixes and some from users who had already uninstalled. Attribution here is genuinely hard.

The monitoring gap#

Crash reporting tools show stack traces. They do not show heap composition. The team had no automated check for retained Activities in CI, no memory regression threshold, and no test device that resembled the median user's phone. All three gaps are cheap to close.

A single UI test that opens and closes the heaviest screen 20 times, then asserts that the retained instance count is zero, would have caught this before release. It runs in a couple of minutes on an emulator.

What the postmortem changed in the release process#

, Device floor testing. Every release now runs a smoke test on a 3 GB or 4 GB RAM device, not just flagship hardware., Leak checks in CI. LeakCanary runs in debug builds automatically, and a retained-activity failure blocks the merge., Memory budgets per screen. Each major screen has a peak heap target measured on the low-end device. Exceeding it by more than 15 percent triggers a review., Callback ownership rules. Any object with application lifetime cannot hold a view, activity, or fragment reference. The team wrote this into its code review checklist, and the linter rule ships with the repo.

The uncomfortable part#

The team spent nine days blaming the camera feature because the crash reports clustered around it. Users who opened the editor were simply more likely to hit the memory ceiling afterward. The lesson is not that profiling tools are magic. It is that the screen where a crash lands is rarely the screen that caused it, and the only reliable way to tell the difference is to look at what the heap is holding.

None of the fixes were exotic. They were the kind of cleanup that gets deferred because the app still launches on the developer's phone. The bill arrives later, on hardware the team never tested.

More articles