本文へスキップ
Skillpedia
モバイルアプリ25k · #10059

java-memory-leaks

できること

このスキルは、Androidアプリが解放すべきメモリを保持し続ける理由を、AIと一緒に見つけます。テストでLeakCanaryのリークレポートを見るAndroid開発者向けです。

使う場面

  • ✓テストのログにLeakCanaryのメモリリークのレポートが出ているとき。
  • ✓Androidの画面やコンポーネントが、閉じた後もメモリに残っているように見えるとき。
  • ✓リークのトレースを読み、解放されるべきオブジェクトを見つける必要があるとき。

スキルのリンク

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

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/chromium/chromium/blob/main/agents/skills/java-memory-leaks/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“Androidのテストで、LeakCanaryのリークが出ています。トレースを読んで、直すのを手伝ってもらえますか。”

毎週5つの新しいスキル。1分で読めます。

仕組み

AIはテストを実行してリークを確認し、ルートから漏れているオブジェクトまでのリークトレースを読みます。クリアされるべき参照を見つけ、修正を提案します。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)を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

よくある質問

メモリリークとは何ですか?

アプリが不要になったオブジェクトをメモリに保持し続けることです。時間とともにアプリを遅くしたり、クラッシュさせたりすることがあります。

LeakCanaryとは何ですか?

Androidアプリのメモリリークを検出し、オブジェクトを何が保持しているかを示すトレースを書くツールです。

このスキルを使うにはJavaを知っている必要がありますか?

はい。このスキルは、Androidの Javaコードを扱います。トレースを読んでコードを変えるのは、AIが行います。

詳細

作者による元の説明

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

repo: chromium/chromium

人気

GitHubのスター
25k
ランキング
#10059
ライセンス
BSD-3-Clause
更新日
2026/11/2

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル