Java memory leaks

From chromium/chromium · View interactive version

Java memory leaks is an AI agent skill from the GitHub repository chromium/chromium. How to identify and fix Java memory leaks in Android Chrome using LeakCanary traces.

How to use this skill

Skill file

Fixing Java Memory Leaks in Android Chrome

This skill guides the process of identifying, debugging, and fixing Java memory

leaks in Android Chrome, typically identified by LeakCanary in instrumentation

tests.

Workflow

  1. Confirm the Leak:

test that exercises the component.

necessary to run the whole test class to reproduce the leak.

lacks @EnableLeakChecks.

fixed, not in PublicTransitLeakTest, as long as that class is in

chrome_public_test_apk and runs on phones. Do not add @EnableLeakChecks

to it: the CQ (android-x64-rel) runs chrome_public_test_apk with

--enable-leak-checks on its phone builder, so every test class there is

leak-checked unless it opts out with @DisableLeakChecks (do not opt out).

Existing uses of @EnableLeakChecks stay; do not add new ones.

unit_device_javatests targets), chrome_public_test_apk_tablet, or tests

restricted to tablet or desktop. If the feature's test class is one of

these, put the leak test in a phone chrome_public_test_apk class instead.

but do not try to fix them unless they seems to be variations of the

suspected leak.

  1. Analyze Leak Trace:

with Leaking: YES).

be broken.

  1. Identify the Break Point:

is destroyed.

observers.

the outer class.

  1. Apply Fix Patterns:

removeObserver() is called in onDestroy() or equivalent lifecycle

teardown.

registering it with ActivityLifecycleDispatcher.

null checks where needed. This is safer and satisfies NullAway without

suppressions.

much "collateral damage" (requiring null checks in many places), you can

use @SuppressWarnings("NullAway") on the onDestroy() method to set

the field to null while keeping it non-null for the rest of the

lifecycle.

```java

private @Nullable CustomTabActivityTabProvider mTabProvider;

@Override

public void onDestroy() {

mTabProvider = null; // Breaks leak trace

}

```

this is done by the owner of the component, not by an observer or

registrar. Observers should not have side effects that mutate the

observed object during teardown.

  1. Verify:

out/Debug/bin/run_chrome_public_test_apk --enable-leak-checks -f "YourLeakTest*".

Best Practices & Gotchas

NullPointerExceptions if other components try to use them during their own

destruction (e.g., to unregister observers). Ensure that cleanup methods like

unregisterObserver handle null references gracefully.

```java

public void unregisterActivityTabObserver(CustomTabTabObserver observer) {

mActivityTabObservers.removeObserver(observer);

if (mTabProvider == null) return; // Guard against NPE during destruction

Tab activeTab = mTabProvider.getTab();

...

}

```

Examples

Nullifying References for Cleanup

Before:

public class MyComponent implements DestroyObserver {
    private final LongLivedProvider mProvider; // Retains activity

    public MyComponent(LongLivedProvider provider) {
        mProvider = provider;
    }

    @Override
    public void onDestroy() {
        // Provider still holds reference to this component, leaking activity
    }
}

After (Preferred):

public class MyComponent implements DestroyObserver {
    private @Nullable LongLivedProvider mProvider;

    public MyComponent(LongLivedProvider provider) {
        mProvider = provider;
    }

    @Override
    public void onDestroy() {
        if (mProvider != null) {
            mProvider.removeObserver(this); // If applicable
            mProvider = null; // Break the leak chain
        }
    }
}
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.