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。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 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
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
常见问题
什么是内存泄漏?
内存泄漏是指应用在不再需要某些对象之后,仍将它们保留在内存中。随着时间推移,这会让应用变慢或崩溃。
什么是 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
评论
- …
同一仓库的更多技能
fuzzing
25k帮你的智能体编写模糊测试,向代码输入大量随机数据,以找出 C++ 代码中的崩溃和错误。
gn-deps-debugging
25k帮你的智能体修复 GN 构建文件中因依赖缺失或错误而导致的构建错误,且不添加范围过大、不必要的依赖。
jni-type-conversion
25k帮你的智能体用更简单的 @JniType 注解,替换 Android 代码中手动的 Java 到 C++ 转换代码。
loadline
25k帮你的智能体运行 LoadLine 基准测试,衡量网页在 Android 和桌面版 Chrome 上的加载速度。