Skip to content
Mai Trinh
  • Android App
  • 18 min read

The ticket said the app used 400 MB because its data lived in RAM. Measuring said otherwise

An 8-sub-ticket refactor plan rested on one assumption: "the data in a static model makes the app heavy". The baseline showed 65% of memory was bitmaps, and moving the main list to a database saved 291 KB. The real wins were an 84.7 MB leak and downloads that couldn't survive process death.

Mai Trinh

Software Engineer

On this page

A case study on refactoring memory and thread management in an Android app with over 60k downloads, and on how the original hypothesis turned out to be almost entirely wrong.

At a glance

  • Problem: the app was often killed while playing music in the background. The Profiler showed it using about 380–400 MB of RAM, making it one of the first apps Android kills. The ticket proposed moving "heavy" data out of a static model class into a database, fixing an Activity leak, and merging background services.
  • Actual cause: the baseline showed about 65% of memory was decoded bitmaps (618–702 MB on a tablet). The Java heap, where that "heavy" model lives, was only 40–80 MB. Moving the main data list to the database saved… about 291 KB.
  • Result: two refactor phases dropped as not worth doing. The real leaks were fixed: LeakCanary 5 → 3 leaks, including an Activity holding 84.7 MB that is now gone. Downloads now resume after the process is killed (0/5 → 3/3). On a low-RAM device, swap during downloads fell 414 → 221 MB (−47%) and peak threads 191 → 176. Idle RAM did not improve, and the report says so plainly.

Starting point

1.1 The app and its context

The app is a "download content to use offline" kind of app: each content package has a few dozen audio files, dozens to hundreds of images, and an offline map region. The home screen is a long feed of large image cards, banners and promo cards with audio trailers. A typical user opens the app, downloads a package, then switches to other apps (maps, camera, messages) while still listening. So the app spends a lot of its life in the background, and being killed there is an everyday event on low-RAM devices.

The codebase is several years old, half Java and half Kotlin. The core classes (main Activity, player, download service) are each between 1,200 and more than 4,000 lines of Java.

1.2 The reported symptoms

The ticket was first opened because the app was frequently killed while playing music in the background: audio stopped mid-listen, and reopening the app meant starting over from scratch.

To find the cause, the rest of the ticket was written after one session with the Android Studio Profiler:

  • The app used about 380–400 MB of RAM. According to the ticket this was "extremely high" and made Android kill the app first whenever it needed memory.
  • When the process was killed, all data was lost: a half-finished download did not continue, and on return some screens were empty or showed errors.
  • Several background flows (download, audio playback, notifications) overlapped and were hard to control.
Android Studio Profiler with the app on the home screen: Total 382.2 MB, Threads 169
The Profiler screenshot behind the ticket. Total 382.2 MB: Java 55.2 MB, Native 73.9 MB, Graphics 83.4 MB, Stack 5.8 MB, Code 119.2 MB, Others 44.7 MB. 169 threads. The two trash-can icons are two GCs.

Looking at this screenshot again after measuring, the answer was already in it:

  • Java is only 55.2 MB, about 14% of the total. If the data in the static model really made the app heavy, this is where it would show.
  • Code at 119.2 MB is the largest part, but it is dex/so/apk mapped into memory, mostly shared and reclaimable by the system. No data refactor can shrink it.
  • Native + Graphics is about 157 MB. That is where decoded bitmaps live, but the Profiler does not break bitmaps out on their own.
  • After each GC the total drops only slightly, then creeps up again. That is a reason to check for leaks, but not enough to say where they are.
  • 169 threads while just sitting on the home screen. That matches the feeling of "overlapping background services".

The problem was not the screenshot. It was that only the total in the corner got read.

1.3 The ticket's three suspected causes

  1. In-memory storage. All important data (the content list, the region list, the downloads list) lived in one model class as member/static fields tied to the Application. When the process is killed, RAM is wiped and the data goes with it.
  2. Memory leak. The class tracking the Activity lifecycle kept a static reference to the current Activity, so Activities were never released, even after closing.
  3. Background services were unoptimized and overlapping.

1.4 The proposed fix and plan

  • Replace in-memory storage with a database (Room or the existing ORM) for heavy data.
  • Merge the background services.
  • Optimize one screen at a time, starting with the home screen. Prioritize the most expensive service (download).

The ticket was then split into 8 assignable sub-tickets:

  • 1.Memory baseline + measuring toolkit

    Kind
    Measure
  • 2.Activity tracker leaks the resumed Activity

    Kind
    Fix
  • 3.Static Context/Activity leaks in singletons

    Kind
    Fix
  • 4.Clean up home screen lifecycle/listeners

    Kind
    Refactor
  • 5.Move the content list from the static model to the DB (phase 1/4)

    Kind
    Refactor
  • 6.Move the region list to the DB (phase 2/4)

    Kind
    Refactor
  • 7.Put download items in the DB, move the downloads list (phase 3/4)

    Kind
    Refactor
  • 8.Move the whole download stack to WorkManager

    Kind
    Refactor

Three of the eight sub-tickets were "move data X to the DB", so the whole plan rested on hypothesis 1.

1.5 Rereading the code, parts no longer matched the ticket

Because it was a large task, the ticket was queued behind a priority app redesign, so work only started a few months later. During that redesign, the devs had converted and refactored part of the Java code to Kotlin to bring the crash rate down. So by the time the ticket came back, the code had changed:

  • The Activity tracker already cleared its reference in onPause. The leak only happened when an Activity never reached onPause (a crash during resume, the process being killed). The ticket described it as worse than it was.
  • The home screen already unregistered from the event bus and no longer held a location client. 3 of the 4 items in sub-ticket 4 had been handled earlier.
  • The Context in Application was still a public var that anyone could reassign. That item still stood.
  • The app set largeHeap="true", so the Java heap was capped at 512 MB on both test devices, but nobody had checked how much Java heap was actually used.

That was the first sign: the ticket described the symptoms correctly, but the "why" had never been measured.

1.6 Why this kind of bug is hard

  • No crash, no obvious ANR, no stack trace.
  • "The app uses a lot of RAM" is a single total that says nothing about which part is heavy.
  • Kills from low memory happen on real low-RAM devices, in the background, out of the developer's sight.
  • "The data is in RAM, so it uses RAM" sounds so right that nobody measures it again.

Measuring a baseline before fixing anything

The first sub-ticket fixed nothing. It only built a baseline and a repeatable measuring setup. It was the best decision of the whole effort.

Devices

  • Model

    Device A
    Galaxy Tab S9 FE
    Device B
    Galaxy A20s
  • Android

    Device A
    16
    Device B
    11
  • RAM

    Device A
    8 GB
    Device B
    3 GB
  • heapgrowthlimit

    Device A
    256m
    Device B
    192m

Build: a real release build (R8 on, debuggable=false), only adding profileable through a throwaway Gradle init script that was never committed:

allprojects {
    plugins.withId('com.android.application') {
        android { buildTypes { release { profileable true } } }
    }
}

4 flows: cold start → home screen idle 30 s; open a content item → play 90 s; download a package; home screen → HOME → 5 minutes in the background → return.

Flow 1 on device A (3 runs):

  • 977–988 MB

    Java heap
    42–47 MB
    Native heap
    661–669 MB
    Graphics
    208–212 MB
    Bitmaps (count / size)
    226–228 / 618–626 MB

Bitmaps were about 65% of total PSS. The Java heap, where the "heavy" in-memory model lives, never went above 80 MB in any flow.

A few more observations:

  • After 5 minutes in the background, bitmaps were still at 607 MB. They were not released, only swapped out. The Application's onTrimMemory / onLowMemory only called super.
  • The whole codebase had one image-load call that set a specific decode size. Everything else decoded at full size.
  • The AppContexts count nearly doubled (about 25 → 42–45) after opening content, downloading or playing. After a warm resume, 3 Activities were alive. Not proof of a leak, but suspicious.
Note: Android 11 does not print the Native Allocations → Bitmap line in dumpsys meminfo, so there are no bitmap numbers for the A20s. That device also swaps heavily to zram, and PSS ranged from 438 to 689 MB between runs. Always read PSS together with Swap.

Which hypotheses were wrong, which were right

  • The content list in the static model makes the app heavy

    Measured
    Moving it to the DB saves about 291 KB
    Decision
    ⏭ Dropped (phase 1)
  • The region list in the static model

    Measured
    about 6 KB
    Decision
    ⏭ Dropped (phase 2)
  • The downloads list is lost when the process is killed

    Measured
    True, and worse: resume after a kill had never worked
    Decision
    ✅ Done, with a narrower scope (phase 3)
  • The Activity tracker leaks Activities

    Measured
    Partly true (see section 4)
    Decision
    ✅ Fixed
  • Static Context/Activity in singletons

    Measured
    True
    Decision
    ✅ Fixed
  • The home screen doesn't clean up listeners

    Measured
    Mostly handled already, but another leak turned up in an adapter
    Decision
    ✅ Fixed
  • Overlapping download services

    Measured
    True: 1 Hz polling, duplicate workers
    Decision
    ✅ Moved to WorkManager

The ticket was right about the symptoms (getting killed, losing state, leaks) but wrong about the cause of the 400 MB figure. Following the ticket to the letter, I would have moved 3 groups of data to a database, written migrations and retested everything, all to save less than 1 MB.

Fixing the leaks

4.1 The Activity tracker holds an Activity in a static

The old code (trimmed):

companion object {
    var currentActivity: Activity? = null
        private set
    private var activityCount = 0
}

override fun onActivityPaused(activity: Activity) {
    if (activity === currentActivity) currentActivity = null
}

override fun onActivityDestroyed(activity: Activity) {
    activityCount--          // reference is not cleared
}

As section 1.5 says, the old code already cleared it in onPause. The leak only happened when an Activity never reached onPause. That is why the numbers after the fix showed no difference in the open/close-screen scenario (section 7).

The new code moves this into a small, testable class. It holds the Activity through a WeakReference and clears it in both onPause and onDestroy:

class CurrentActivityRegistry {
    private var currentRef: WeakReference<Activity>? = null
    private var aliveCount = 0

    val current: Activity? get() = currentRef?.get()

    fun onResumed(activity: Activity) { currentRef = WeakReference(activity) }
    fun onPaused(activity: Activity) = clearIfCurrent(activity)
    fun onDestroyed(activity: Activity) {
        if (aliveCount > 0) aliveCount--
        clearIfCurrent(activity)
    }

    private fun clearIfCurrent(activity: Activity) {
        if (current === activity) currentRef = null
    }
}

4.2 The mini-player singleton holds an Activity Context

The floating mini-player is a singleton, and it kept the Context passed in at show():

private Context context;
private static MiniPlayerManager instance;

public void show(Context context) {
    this.context = context;   // Activity context, lives as long as the singleton
    ...
}

There were two near copy-paste managers (about 280 and 250 lines of Java). I merged them into one Kotlin base class: the View takes its context from itself (floatingView.context), and commands to the service go through the Application context. Each subclass is now about 75–80 lines.

4.3 An adapter with a static Context that creates a new MediaPlayer on every bind

private static Context context;
private static MediaPlayer mPlayer = null;
...
// in onBind of a card with a trailer
MediaPlayer player = new MediaPlayer();   // never released

Every time a trailer card was rebound while scrolling, another native MediaPlayer was created and never released. The fix:

  • Each adapter has its own trailer player, holding the Application context.
  • The player is created on demand and release()d when the adapter is torn down.
  • The card's View subscribes through a listener. The listener is replaced on every bind and cleared on release, so the player never holds a View.

4.4 An 84.7 MB leak nobody meant to write

This was the biggest leak LeakCanary found, and it was somewhere nobody thought to look:

// MainActivity.onCreate
KeyboardUtils.addKeyboardToggleListener(this, isVisible -> {
//  No need to handle this here, each screen handles it itself
//  showBottomTabbar(!isVisible)
});

The callback was empty (its logic had been commented out long ago), but the registration was still there and never removed. The helper kept listeners in a static map, the listener held the root view, and the root view held MainActivity. After every recreate (for example, switching dark mode), the entire old Activity, 84.7 MB, stayed in RAM.

The fix: while converting this Activity to Kotlin, delete the registration line. No WeakReference, no unregister: a callback that does nothing should not exist.

Making downloads survive process death

This is the part the ticket called "in-memory storage", and it turned out to be the most valuable part.

5.1 A pre-existing bug: resume had never worked

The app had a "if a download was in progress, reopen the download screen on launch" mechanism. But it called a DB function to fetch the content to resume, and that function only returned fully downloaded content. Content mid-download → null → no resume. The flow that restores an interrupted purchase shares this path, so it was very likely broken too.

Instead of moving the whole downloads list to the DB as the ticket proposed, I narrowed the scope: store only what resume needs (the id of the content being downloaded and a finished flag) in preferences, and put the logic in a plain, testable object:

object DownloadResume {
    fun markStarted(id: String?, type: Int) { /* save id, finished = false */ }
    fun markFinished() { /* finished = true, clear id */ }

    // Partial content not yet in the DB → build a stub with the id; the download screen refetches details from the API
    fun loadToResume(id: String?): Content? {
        if (id.isNullOrEmpty()) return null
        // Runs in the main Activity's onCreate: an exception here would crash EVERY app launch while the flag is set
        return try { /* read DB */ } catch (e: Exception) { stubOf(id) }
    }
}

The download worker already skips files whose MD5 matches, so re-enqueuing everything is enough: files already downloaded are not fetched again.

5.2 Moving the download stack to WorkManager

Before the fix:

  • A Handler polled the status every second (1 Hz), plus a loop on the main thread waiting for the map download to finish.
  • Restarting a download (the stuck-download watchdog, resume) could run two sets of workers on the same file.
  • Stopping a download called WorkManager.cancelAllWork(), which also cancelled unrelated work (battery warnings, onboarding notifications).

After the fix:

// REPLACE: a restart (watchdog, resume after a kill) drops the previous run's workers
WorkManager.getInstance(this).enqueueUniqueWork(
    uniqueWorkName(contentId), ExistingWorkPolicy.REPLACE, requests
)

// Cancel only this service's file-download work
WorkManager.getInstance(this).cancelAllWorkByTag(FILE_DOWNLOAD_TAG)

Polling is gone, and completion checks are now event-driven. The app also resumes downloading on its own when the network comes back, instead of making the user tap "restart download".

Measuring: tools and repeatable scenarios

6.1 Comparing the right pair of builds

  • base

    What it is
    the release branch right before the first sub-ticket (same point as the baseline)
  • after

    What it is
    all 8 sub-tickets + the main branch merged in
  • production

    What it is
    for reference only, not used as "before" because it lacks the release branch's features

All three are debug builds: absolute numbers are higher than release, so only builds of the same type are compared.

6.2 Snapshotting the signed-in state

Every run has to start from exactly the same state. I signed in once, snapshotted the app's data directory to a .tar, and restored it before every run. The three content packages were always downloaded in the same order.

6.3 Measuring leaks: force a GC before reading

The scenario: from the home screen, loop N rounds of open a detail page → Back, open a preview for 6 s → Back, scroll past the promo cards. At rounds 0, 5 and 10, force a GC (on the A20s with am dumpheap -g) and only then measure. What remains is what is being held, not garbage waiting to be collected.

6.4 Simulating process death

  • Emulator: kill -9, exactly what the system does.
  • A20s (Android 11): SELinux blocks run-as from sending signals (Permission denied), and with am crash the app stays alive, so am force-stop it is. Force-stop is harsher (it also cancels pending jobs), but it was applied identically to both builds.

6.5 Lessons about measuring

  • The Views count in dumpsys meminfo is not a leak count. It includes views RecyclerView keeps around to reuse while scrolling.
  • The scenario never touched the leak that was fixed. Detail pages open inside the main Activity, so the whole scenario only ever had 2 Activities. Seeing an Activity leak means opening and closing a separate Activity many times.
  • The script broke its own scenario. Partway through, the script's own swipes dragged the mini-player into the middle of the screen, so some swipes stopped scrolling. It happened on both builds, so the comparison stays fair, but it had to be written down.
  • The host machine is a variable too. The first LeakCanary run on base hit an ANR at cold start because the emulator was overloaded right after installing a 250 MB APK (load average around 16). The script now waits for load < 4 before launching.

Final results

7.1 Leaks, measured with meminfo: no visible improvement

A20s, 10 rounds of opening/closing screens, measured after GC:

  • Views

    base
    +3179
    after
    +3136
    Δ
    −1%
  • Threads

    base
    +68
    after
    +63
    Δ
    −7%
  • PSS + Swap

    base
    +626.5 MB
    after
    +505.3 MB
    Δ
    −19% (1 run, not conclusive)

The round-1 reviewer pointed exactly at this: the leak fixes produced no measurable change. They asked for a LeakCanary run or a heap dump.

7.2 Leaks, measured with LeakCanary: a real improvement

leakcanary-android:2.14, retainedVisibleThreshold = 1000 (dump only once, when the app goes to the background), the same long flow for both builds:

  • Application leaks

    base
    5
    after
    3
  • Library leaks

    base
    0
    after
    0
  • Main Activity ← keyboard helper's static map

    base
    84.7 MB
    after
    gone
  • Download Activity ← mini-player singleton .context

    base
    264 KB
    after
    gone
  • Mini-player View ← mini-player singleton

    base
    8.6 KB
    after
    gone
  • Music service ← static controller's .listeners

    base
    85 KB
    after
    85 KB
  • Detail Fragment ← MapView in a ViewHolder ← map SDK render thread

    base
    23.3 MB
    after
    27.2 MB
  • Main Activity after recreate ← map controller (same map SDK path)

    base
    hidden ¹
    after
    18.1 MB

¹ LeakCanary reports only the shortest path to each object. On base, the main Activity was reported through the keyboard helper, so the map SDK path behind it did not show separately.

7.3 Process death and network loss

  • Process killed mid-download (emulator)

    production
    no resume, ANR 2/2
    base
    no resume 2/2
    after
    resumed after 43 s, 52 s, no ANR
  • Force-stop mid-download (A20s)

    production
    —
    base
    no resume
    after
    resumed, finished in 142 s
  • Network off for 30 s mid-download (A20s)

    production
    resumed after 135 s
    base
    no resume
    after
    resumed after 123 s

The production and base ANR on reopening after a kill: the main thread was stuck in a continuous measure/layout loop in the main Activity, about 16 s of CPU. It no longer happens on after.

7.4 Downloading 3 content packages on a low-RAM device (A20s)

  • Peak PSS

    base
    735.5 MB
    after
    710.6 MB
    Δ
    −3.4%
  • Peak swap

    base
    413.6 MB
    after
    220.8 MB
    Δ
    −46.6%
  • PSS + Swap after the 3rd package

    base
    1084.1 MB
    after
    894.6 MB
    Δ
    −17.5%
  • Peak threads

    base
    191
    after
    176
    Δ
    −7.9%
  • 60 s in the background: Swap / threads

    base
    412.5 MB / 180
    after
    244.9 MB / 166
    Δ
    −40.6% / −7.8%

On the emulator (plenty of RAM, no swap), memory used for downloading was almost the same (+131.9 MB vs +138.2 MB). The gains only show on a device that is actually short on RAM.

7.5 Unchanged, and rightly so

  • Cold start

    Emulator base → after
    4793 → 4558 ms
    A20s base → after
    7329 → 7151 ms
  • Home screen PSS, idle 30 s

    Emulator base → after
    279 → 307 MB (range 245–316)
    A20s base → after
    643 → 634 MB

The ticket's "400 MB" figure did not go down, because this refactor never touched bitmaps. I said so plainly in the report instead of picking flattering numbers.

Review rounds and QA (and the bugs they found)

The PR went through 5 review rounds. Round 1 found 1 critical and 4 medium issues. The most notable:

  • When a download finished in the background, the finished flag was never set (only the download screen cleared it). On the next cold start the app downloaded everything again and re-fired the analytics events of the purchase/download funnel. Fix: clear the flag in the service as soon as the content is saved.
  • Logout did not clear the resume state, so the next user would resume the previous user's content.
  • The function reading the content to resume called the DB outside the try, so one DB error would crash every app launch.

Then device QA across 4 scenarios surfaced one more bug, already present on base:

HOME while on the download screen → download finishes in the background → process is killed → reopen the app. Android recreates the download Activity from saved state. The in-memory data is gone, so the screen reruns the purchase check, fails to fetch details, and shows a "couldn't load the details" error.

Clearing the resume flag was not enough, because this path never goes through the resume flag: it is Android restoring the back stack on its own. Fix: save the content id in onSaveInstanceState. On recreate, close the screen if the flag is finished and the content is already in the DB:

fun shouldCloseRecreatedScreen(
    isRecreated: Boolean, downloadFinished: Boolean, isDownloaded: () -> Boolean
): Boolean = isRecreated && downloadFinished && isDownloaded()

A config change or a kill in the middle of a download keeps the screen, because the flag is still unfinished.

  • Download finishes in the background → kill → reopen

    after (before the fix)
    prefs already clean, but the download screen still reopens with an error
    after + fix
    back to the home screen, no dialog
  • kill -9 6 s into a download

    after (before the fix)
    resumed, finished after 71 s, each analytics event ×1
    after + fix
    resumed, finished after 95 s, ×1
  • EDGE network (57.8 KB/s)

    after (before the fix)
    finished after 43 s, no "stuck" popup
    after + fix
    —
  • Offline map download fails

    after (before the fix)
    kept the "Start" button for 9 minutes, no popup
    after + fix
    —
Small tip: to simulate the map SDK failing to download, blocking the domain's SNI string is not enough. You have to reject the CDN IP addresses that domain resolves to.

Unit tests went from 313 to 372, all passing.

What's left

  • Bitmaps: 65% of memory, and still around after 5 minutes in the background. That is the topic of the next post: 400 MB of bitmaps in a sleeping app.
  • The map SDK's MapView leak: every detail page opened leaves a fragment behind (about 23–27 MB). A recreate leaves the whole main Activity behind (about 18 MB).
  • The music service registers a listener on a static controller and never removes it; a one-line fix.

Takeaways

  1. Measure a baseline before refactoring. One dumpsys meminfo table removed 2 of 8 sub-tickets and changed the direction of the whole effort. A ticket written from a single total can always be wrong about the "why".
  2. Reread the code before following an old ticket. A few months is enough for half of a ticket's description to stop being true.
  3. Break the total down. Split PSS into Java heap / native / graphics / bitmaps from the start.
  4. Your measuring tool has to reach what you fixed. My meminfo scenario never opened a separate Activity, so it could not see the fixed Activity leak. LeakCanary could.
  5. The biggest leak often lives in dead code. An empty callback registered in a static map held 84.7 MB. Code review didn't catch it because "it doesn't do anything".
  6. Process death is a main flow, not an edge case. Resume had never worked, and Android recreating Activities from the back stack takes a completely different path. Test with kill -9, not with the Back button.
  7. Thread/swap gains only show on weak devices. An emulator with spare RAM will tell you "no difference".
  8. Report honestly, including what didn't improve. The report said "no improvement in this scenario" right next to "−47% swap", and that honesty is what made the reviewer trust the rest.

AI helped a lot with writing the measuring scripts, converting Java → Kotlin one method at a time, and reviewing each round. But the question "what is that 400 MB really made of" had to be asked before writing a single line of code.

Appendix: detailed numbers

A. Baseline, flow 1 (cold start → home screen idle 30 s)

  • A

    Run
    1
    Start
    755 ms
    Total PSS
    977 MB
    Java
    42 MB
    Native
    661 MB
    Graphics
    211 MB
    Bitmaps
    226 / 618 MB
    Swap
    0.3 MB
  • A

    Run
    2
    Start
    405 ms
    Total PSS
    986 MB
    Java
    47 MB
    Native
    669 MB
    Graphics
    208 MB
    Bitmaps
    228 / 626 MB
    Swap
    0.3 MB
  • A

    Run
    3
    Start
    356 ms
    Total PSS
    988 MB
    Java
    44 MB
    Native
    669 MB
    Graphics
    212 MB
    Bitmaps
    228 / 626 MB
    Swap
    0.3 MB
  • B

    Run
    1
    Start
    2012 ms
    Total PSS
    532 MB
    Java
    34 MB
    Native
    368 MB
    Graphics
    55 MB
    Bitmaps
    n/a
    Swap
    24 MB
  • B

    Run
    2
    Start
    1651 ms
    Total PSS
    689 MB
    Java
    39 MB
    Native
    109 MB
    Graphics
    55 MB
    Bitmaps
    n/a
    Swap
    458 MB
  • B

    Run
    3
    Start
    4975 ms
    Total PSS
    438 MB
    Java
    40 MB
    Native
    286 MB
    Graphics
    55 MB
    Bitmaps
    n/a
    Swap
    7 MB

B. Baseline, flow 4 (home screen → HOME → 5 minutes → return), device A

  • Home screen, before HOME

    Total PSS
    974 MB
    Graphics
    208 MB
    Bitmaps
    226 / 618 MB
    Swap
    0.3 MB
  • 5 s in the background

    Total PSS
    819 MB
    Graphics
    61 MB
    Bitmaps
    224 / 607 MB
    Swap
    —
  • 5 minutes in the background

    Total PSS
    743 MB
    Graphics
    1 MB
    Bitmaps
    224 / 607 MB
    Swap
    680 MB
  • Back (warm, 220 ms) + 10 s

    Total PSS
    1085 MB
    Graphics
    217 MB
    Bitmaps
    248 / 702 MB
    Swap
    55 MB

C. Object counts (dumpsys meminfo → Objects)

  • Home screen idle

    AppContexts (A / B)
    23–25 / 26–28
    Activities (A / B)
    1–2 / 2
  • After downloading

    AppContexts (A / B)
    42 / 45
    Activities (A / B)
    2 / 3
  • After 90 s of playback

    AppContexts (A / B)
    43 / 45
    Activities (A / B)
    3 / 3
  • After warm resume

    AppContexts (A / B)
    27 / 27
    Activities (A / B)
    3 / 2

D. Leak detail on the A20s (after GC)

  • Views

    base r0
    510
    base r5
    2136
    base r10
    3689
    after r0
    510
    after r5
    2091
    after r10
    3646
  • Threads

    base r0
    115
    base r5
    162
    base r10
    183
    after r0
    122
    after r5
    164
    after r10
    185
  • Java heap (MB)

    base r0
    29.6
    base r5
    42.9
    base r10
    50.7
    after r0
    30.4
    after r5
    43.9
    after r10
    56.1
  • PSS (MB)

    base r0
    670.6
    base r5
    777.1
    base r10
    831.3
    after r0
    612.9
    after r5
    709.1
    after r10
    778.4
  • Swap (MB)

    base r0
    51.7
    base r5
    341.7
    base r10
    517.5
    after r0
    44.3
    after r5
    311.0
    after r10
    384.1

E. Summary

  • Sub-tickets done

    Before
    8
    After
    6 (2 dropped after measuring)
  • LeakCanary application leaks

    Before
    5
    After
    3
  • Resume after process kill

    Before
    0/5
    After
    3/3
  • Peak swap while downloading (A20s)

    Before
    413.6 MB
    After
    220.8 MB
  • Peak threads while downloading (A20s)

    Before
    191
    After
    176
  • Unit tests

    Before
    313
    After
    372
  • Code

    Before
    —
    After
    about 1.9k lines of dead/duplicate code removed; 4 core classes Java → Kotlin
  • Memory leak
  • LeakCanary
  • WorkManager
  • Process death
  • Refactor
  • Measurement

Keep reading

All posts