java-memory-leaks
Was er macht
Dieser Skill führt deine KI durch die Suche, warum eine Android-App Speicher behält, den sie freigeben sollte. Er ist für Android-Entwickler gedacht, die Lecks-Berichte in ihren Tests sehen.
Wann du ihn nutzt
- ✓Wenn ein Testlog einen Speicherleck-Bericht von LeakCanary zeigt.
- ✓Wenn ein Android-Bildschirm oder eine Komponente nach dem Schließen im Speicher zu bleiben scheint.
- ✓Wenn du eine Leck-Spur lesen und das Objekt finden musst, das freigegeben werden sollte.
Link zum Skill
https://github.com/chromium/chromium/blob/main/agents/skills/java-memory-leaks/SKILL.mdKopiere ihn und füge ihn in deine KI ein (Claude, ChatGPT oder Cursor) mit der Bitte, den Anweisungen zu folgen.
So nutzt du ihn
Am einfachsten: Kopiere die Nachricht und füge sie in deine KI ein. Sie liest den Skill und folgt den Anweisungen.
Fertige Nachricht zum Einfügen
Für Claude Code, Codex oder Cursor: Führe den Befehl in deinem Projektordner aus, dann wird der Skill installiert.
Lade die SKILL.md herunter und lege sie in den Skills-Ordner deines Agenten.
Beispielanfrage an deine KI
“Mein Android-Test zeigt ein LeakCanary-Leck. Kannst du die Spur lesen und mir helfen, es zu beheben?”
5 neue Skills pro Woche. 1 Minute Lesezeit.
So funktioniert es
Die KI bestätigt das Leck, indem sie den Test ausführt, liest die Leck-Spur von der Wurzel bis zum lecken Objekt, findet die Referenz, die gelöst werden sollte, und schlägt eine Korrektur vor.
Technischer Inhalt (originale SKILL.md)
Originaltext des Autors, auf Englisch, direkt von GitHub.
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
Confirm the Leak:
- If you suspect a leak, confirm it by adding or running an instrumentation test that exercises the component.
- If the leak does not reproduce and the test has
@Batch, it may be necessary to run the whole test class to reproduce the leak. - Critical: Pass
--enable-leak-checksto the test runner if the class lacks@EnableLeakChecks. - Run the test and look for LeakCanary detections in the log output.
- Do not get sidetracked by other crashes, or even other leaks. Report these, but do not try to fix them unless they seems to be variations of the suspected leak.
Analyze Leak Trace:
- Examine the LeakCanary trace from the test failure log.
- Identify the leaking object (usually at the bottom of the trace, marked
with
Leaking: YES). - Identify the GC Root (at the top of the trace).
- Trace the retention path from the GC Root to the leaking object.
- Look for
Leaking: UNKNOWNnodes in between to find where the chain should be broken.
Identify the Break Point:
- Look for references that should be cleared when the activity or component is destroyed.
- Common culprits:
- Observers not unregistered.
- Missing calls to
destroy()on components that hold resources or observers. - Static variables holding references to activities or contexts.
- Inner classes (especially anonymous ones) holding implicit references to the outer class.
- Callbacks passed to long-lived components without lifecycle management.
Apply Fix Patterns:
- Pattern 1: Explicit Unregistration:
- If the leak is due to an observer not being removed, ensure
removeObserver()is called inonDestroy()or equivalent lifecycle teardown. - If the component is not a
DestroyObserver, consider making it one and registering it withActivityLifecycleDispatcher.
- If the leak is due to an observer not being removed, ensure
- Pattern 2: Nullifying References:
- If a field causes leaks after destruction, nullify it in
onDestroy(). - Preferred approach: Mark the field as
@Nullableand add explicit null checks where needed. This is safer and satisfies NullAway without suppressions. - Alternative approach: If marking the field
@Nullablecauses too much "collateral damage" (requiring null checks in many places), you can use@SuppressWarnings("NullAway")on theonDestroy()method to set the field tonullwhile keeping it non-null for the rest of the lifecycle. - Example (Preferred):
private @Nullable CustomTabActivityTabProvider mTabProvider; @Override public void onDestroy() { mTabProvider = null; // Breaks leak trace }
- If a field causes leaks after destruction, nullify it in
- Pattern 3: Avoid Mutation in Observers:
- If cleanup requires mutating state (e.g., calling
removeTab()), ensure 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.
- If cleanup requires mutating state (e.g., calling
- Pattern 1: Explicit Unregistration:
Verify:
- Rebuild the test target:
autoninja -C out/Debug chrome_public_test_apk. - Run the specific leak test:
out/Debug/bin/run_chrome_public_test_apk --enable-leak-checks -f "YourLeakTest*". - Verify that LeakCanary no longer reports the leak.
- Trybot Verification: To run the LeakCanary bot on CQ / trybots,
include the following footer in the CL description:
Cq-Include-Trybots: luci.chromium.try:android-16-x64-leakcanary-rel
- Rebuild the test target:
Best Practices & Gotchas
- Order of Destruction: Nullifying references in
onDestroy()can causeNullPointerExceptions if other components try to use them during their own destruction (e.g., to unregister observers). Ensure that cleanup methods likeunregisterObserverhandle null references gracefully.public void unregisterActivityTabObserver(CustomTabTabObserver observer) { mActivityTabObservers.removeObserver(observer); if (mTabProvider == null) return; // Guard against NPE during destruction Tab activeTab = mTabProvider.getTab(); ... }
Examples
Nullifying References
Häufige Fragen
Was ist ein Speicherleck?
Ein Speicherleck entsteht, wenn eine App Objekte im Speicher behält, die sie nicht mehr braucht. Mit der Zeit kann das die App verlangsamen oder zum Absturz bringen.
Was ist LeakCanary?
LeakCanary ist ein Werkzeug, das Speicherlecks in Android-Apps erkennt und eine Spur schreibt, die zeigt, was das Objekt festhält.
Muss ich Java können, um das zu nutzen?
Ja, der Skill arbeitet mit Java-Code auf Android. Die Auswertung der Spur und die Codeänderungen übernimmt deine KI.
Details
Originalbeschreibung des Autors
How to identify and fix Java memory leaks in Android Chrome using LeakCanary traces.
repo: chromium/chromium
Beliebtheit
- GitHub-Sterne
- 25k
- Platz im Ranking
- #10059
- Lizenz
- BSD-3-Clause
- Aktualisiert
- 2.11.2026
Kommentare
- …
Weitere Skills aus diesem Repository
fuzzing
25kHilft deinem Agenten, Fuzz-Tests zu schreiben, die viele zufällige Eingaben in Code einspeisen, um Abstürze und Fehler in C++-Code zu finden.
gn-deps-debugging
25kHilft deinem Agenten, Build-Fehler zu beheben, die durch fehlende oder falsche Abhängigkeiten in GN-Build-Dateien entstehen, ohne breite, unnötige Abhängigkeiten hinzuzufügen.
jni-type-conversion
25kHilft deinem Agenten, manuellen Java-zu-C++-Umwandlungscode durch einfachere @JniType-Annotationen in Android-Code zu ersetzen.
loadline
25kHilft deinem Agenten, LoadLine-Benchmarks auszuführen, die messen, wie schnell Webseiten auf Android und in Chrome am Desktop laden.
Ähnliche Skills
android-clean-architecture
280kLässt deinen Agenten Android-Projekte effizient strukturieren. Der Code wird so organisiert, dass Wartung und neue Features leichter werden.
compose-multiplatform-patterns
280kHilft deinem Agenten, Compose Multiplatform und Jetpack Compose Patterns in KMP-Projekten einzusetzen. Abgedeckt sind State, Navigation, Theming und Performance.
dart-flutter-patterns
280kLässt deinen Agenten Code für Dart- und Flutter-Apps schreiben. Er nutzt moderne Patterns für robuste und gut wartbare Apps.