跳到正文
Skillpedia
移动应用25k · #10059

java-memory-leaks

它能做什么

这个技能指导你的 AI 找出 Android 应用为何一直占用本应释放的内存。适合在测试中看到泄漏报告的 Android 开发者。

适用场景

  • ✓当测试日志中出现 LeakCanary 内存泄漏报告时。
  • ✓当某个 Android 界面或组件在关闭后似乎仍保留在内存中时。
  • ✓当你需要读取泄漏追踪,并找出应被清除的对象时。

技能链接

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

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/chromium/chromium/blob/main/agents/skills/java-memory-leaks/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 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?

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

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能