Aller au contenu
Skillpedia
Applis mobiles25k · #10059

java-memory-leaks

Ce qu’il fait

Cette compétence guide votre IA pour comprendre pourquoi une application Android conserve une mémoire qu'elle devrait libérer. Elle s'adresse aux développeurs Android qui voient des rapports de fuite dans leurs tests.

Quand l’utiliser

  • ✓Quand un journal de test affiche un rapport de fuite de mémoire LeakCanary.
  • ✓Quand un écran ou un composant Android semble rester en mémoire après sa fermeture.
  • ✓Quand vous devez lire une trace de fuite et trouver l'objet qui devrait être libéré.

Lien du skill

https://github.com/chromium/chromium/blob/main/agents/skills/java-memory-leaks/SKILL.md

Copiez-le et collez-le dans votre IA (Claude, ChatGPT ou Cursor) en lui demandant de suivre les instructions.

Comment l’utiliser

Le plus simple : copiez le message et collez-le dans votre IA. Elle lit le skill et suit les instructions.

Message prêt à coller

Utilise ce skill pour m’aider : https://github.com/chromium/chromium/blob/main/agents/skills/java-memory-leaks/SKILL.md Lis le fichier SKILL.md de ce lien et suis ses instructions pour ma tâche.
Envie que votre IA trouve seule des skills comme celui-ci ?Installez le MCP gratuit de Skillpedia : une ligne, et elle cherche parmi 15 000+ skills avant chaque tâche.Installer en 1 minute →

Exemple de demande à votre IA

“Mon test Android affiche une fuite LeakCanary. Pouvez-vous lire la trace et m'aider à la corriger ?”

5 nouveaux skills par semaine. 1 minute de lecture.

Comment ça marche

L'IA confirme la fuite en lançant le test, lit la trace de fuite depuis la racine jusqu'à l'objet qui fuit, repère la référence qui devrait être libérée, puis propose une correction.

Contenu technique (SKILL.md original)

Texte original de l’auteur, en anglais, directement depuis 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

  1. 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-checks to 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.
  2. 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: UNKNOWN nodes in between to find where the chain should be broken.
  3. 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.
  4. Apply Fix Patterns:

    • Pattern 1: Explicit Unregistration:
      • If the leak is due to an observer not being removed, ensure removeObserver() is called in onDestroy() or equivalent lifecycle teardown.
      • If the component is not a DestroyObserver, consider making it one and registering it with ActivityLifecycleDispatcher.
    • Pattern 2: Nullifying References:
      • If a field causes leaks after destruction, nullify it in onDestroy().
      • Preferred approach: Mark the field as @Nullable and add explicit null checks where needed. This is safer and satisfies NullAway without suppressions.
      • Alternative approach: If marking the field @Nullable causes too 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.
      • Example (Preferred):
        private @Nullable CustomTabActivityTabProvider mTabProvider;
        
        @Override
        public void onDestroy() {
            mTabProvider = null; // Breaks leak trace
        }
        
    • 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.
  5. 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

Best Practices & Gotchas

  • Order of Destruction: Nullifying references in onDestroy() can cause 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.
    public void unregisterActivityTabObserver(CustomTabTabObserver observer) {
        mActivityTabObservers.removeObserver(observer);
        if (mTabProvider == null) return; // Guard against NPE during destruction
        Tab activeTab = mTabProvider.getTab();
        ...
    }
    

Examples

Nullifying References

Questions fréquentes

Qu'est-ce qu'une fuite de mémoire ?

Une fuite de mémoire se produit lorsqu'une application conserve des objets en mémoire alors qu'elle n'en a plus besoin. Avec le temps, cela peut ralentir l'application ou la faire planter.

Qu'est-ce que LeakCanary ?

LeakCanary est un outil qui détecte les fuites de mémoire dans les applications Android et écrit une trace montrant ce qui retient l'objet.

Faut-il connaître Java pour l'utiliser ?

Oui. La compétence fonctionne avec du code Java sur Android. Votre IA se charge de lire la trace et d'effectuer les modifications de code.

Détails

Description originale de l’auteur

How to identify and fix Java memory leaks in Android Chrome using LeakCanary traces.

repo: chromium/chromium

Popularité

Étoiles GitHub
25k
Position au classement
#10059
Licence
BSD-3-Clause
Mis à jour
02/11/2026

Ce skill vous est-il utile ?

0

Connectez-vous pour voter.

Commentaires

  • …

Autres skills de ce dépôt

Skills similaires