java-memory-leaks
Skill này làm gì
Skill này hướng dẫn AI của bạn tìm vì sao một ứng dụng Android giữ bộ nhớ mà lẽ ra phải giải phóng. Skill dành cho lập trình viên Android thấy báo cáo rò rỉ trong kiểm thử.
Khi nào nên dùng
- ✓Khi nhật ký kiểm thử hiển thị một báo cáo rò rỉ bộ nhớ LeakCanary.
- ✓Khi một màn hình hoặc thành phần Android có vẻ vẫn nằm trong bộ nhớ sau khi đóng.
- ✓Khi bạn cần đọc vết rò rỉ và tìm đối tượng cần được giải phóng.
Liên kết skill
https://github.com/chromium/chromium/blob/main/agents/skills/java-memory-leaks/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Bài kiểm thử Android của tôi hiển thị một rò rỉ LeakCanary. Bạn có thể đọc vết và giúp tôi sửa không?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
AI xác nhận rò rỉ bằng cách chạy kiểm thử, đọc vết rò rỉ từ gốc đến đối tượng bị rò, tìm tham chiếu cần được xóa và đề xuất cách sửa.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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
Câu hỏi thường gặp
Rò rỉ bộ nhớ là gì?
Rò rỉ bộ nhớ xảy ra khi ứng dụng giữ các đối tượng trong bộ nhớ sau khi không còn cần chúng. Theo thời gian điều này có thể làm ứng dụng chậm hoặc bị crash.
LeakCanary là gì?
LeakCanary là công cụ phát hiện rò rỉ bộ nhớ trong ứng dụng Android và ghi lại vết cho thấy thứ gì đang giữ đối tượng.
Tôi có cần biết Java để dùng không?
Có. Skill làm việc với mã Java trên Android. AI của bạn đọc vết và thực hiện thay đổi mã.
Chi tiết
Mô tả gốc của tác giả
How to identify and fix Java memory leaks in Android Chrome using LeakCanary traces.
repo: chromium/chromium
Độ phổ biến
- Sao GitHub
- 25k
- Vị trí xếp hạng
- #10059
- Giấy phép
- BSD-3-Clause
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
fuzzing
25kGiúp agent của bạn viết các bài kiểm thử fuzz, đưa nhiều đầu vào ngẫu nhiên vào mã để tìm lỗi crash và bug trong mã C++.
gn-deps-debugging
25kGiúp agent của bạn sửa lỗi build do phụ thuộc thiếu hoặc sai trong file build GN, mà không thêm các phụ thuộc rộng và không cần thiết.
jni-type-conversion
25kGiúp agent của bạn thay thế mã chuyển đổi Java sang C++ thủ công bằng các chú thích @JniType đơn giản hơn trong mã Android.
loadline
25kGiúp agent của bạn chạy các benchmark LoadLine đo tốc độ tải trang web trên Android và Chrome máy tính để bàn.
Skill tương tự
android-clean-architecture
280kGiúp agent của bạn tổ chức dự án Android hiệu quả, cấu trúc code để việc bảo trì và phát triển tính năng mới dễ dàng hơn.
compose-multiplatform-patterns
280kGiúp agent của bạn dùng các mẫu Compose Multiplatform và Jetpack Compose trong dự án KMP, gồm trạng thái, điều hướng, giao diện và hiệu năng.
dart-flutter-patterns
280kGiúp agent của bạn viết code cho ứng dụng Dart và Flutter. Agent dùng các mẫu hiện đại để tạo ứng dụng vững chắc và dễ quản lý.