java-memory-leaks
Fungsinya
Skill ini memandu AI Anda untuk mencari tahu mengapa aplikasi Android terus menyimpan memori yang seharusnya dilepas. Cocok untuk pengembang Android yang melihat laporan kebocoran dalam tes mereka.
Kapan dipakai
- βSaat log tes menampilkan laporan kebocoran memori LeakCanary.
- βSaat layar atau komponen Android tampaknya tetap ada di memori setelah ditutup.
- βSaat Anda perlu membaca jejak kebocoran dan menemukan objek yang seharusnya dibersihkan.
Link skill
https://github.com/chromium/chromium/blob/main/agents/skills/java-memory-leaks/SKILL.mdSalin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.
Cara pakai
Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.
Pesan siap tempel
Untuk Claude Code, Codex, atau Cursor: jalankan di folder proyek, lalu skill langsung terpasang.
Unduh SKILL.md lalu taruh di folder skill agen Anda.
Contoh permintaan ke AI
βTes Android saya menampilkan kebocoran LeakCanary. Bisakah Anda membaca jejaknya dan membantu memperbaikinya?β
5 skill baru setiap minggu. 1 menit baca.
Cara kerjanya
AI memastikan kebocoran dengan menjalankan tes, membaca jejak kebocoran dari akar sampai objek yang bocor, menemukan referensi yang seharusnya dihapus, lalu mengusulkan perbaikan.
Konten teknis (SKILL.md asli)
Teks asli dari pembuat, dalam bahasa Inggris, langsung dari 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
Pertanyaan umum
Apa itu kebocoran memori?
Kebocoran memori terjadi ketika aplikasi menyimpan objek di memori padahal sudah tidak diperlukan. Seiring waktu, hal ini bisa memperlambat aplikasi atau membuatnya crash.
Apa itu LeakCanary?
LeakCanary adalah alat yang mendeteksi kebocoran memori di aplikasi Android dan menulis jejak yang menunjukkan apa yang menahan objek tersebut.
Apakah saya perlu tahu Java untuk memakai ini?
Ya, skill bekerja dengan kode Java di Android. AI Anda yang membaca jejak dan membuat perubahan kode.
Detail
Deskripsi asli dari pembuat
How to identify and fix Java memory leaks in Android Chrome using LeakCanary traces.
repo: chromium/chromium
Popularitas
- Bintang GitHub
- 25k
- Peringkat
- #10059
- Lisensi
- BSD-3-Clause
- Diperbarui
- 2/11/2026
Komentar
- β¦
Skill lain dari repositori ini
fuzzing
25kMembantu agent Anda menulis tes fuzz yang memasukkan banyak input acak ke dalam kode untuk menemukan crash dan bug pada kode C++.
gn-deps-debugging
25kMembantu agent Anda memperbaiki error build akibat dependensi yang hilang atau salah pada file build GN, tanpa menambahkan dependensi luas yang tidak diperlukan.
jni-type-conversion
25kMembantu agent Anda mengganti kode konversi Java ke C++ yang ditulis manual dengan anotasi @JniType yang lebih sederhana pada kode Android.
loadline
25kMembantu agent Anda menjalankan benchmark LoadLine yang mengukur seberapa cepat halaman web dimuat di Chrome Android dan desktop.
Skill serupa
android-clean-architecture
280kMembuat agent Anda mengatur proyek Android dengan efisien. Struktur kodenya dibuat agar lebih mudah dirawat dan pengembangan fitur baru.
compose-multiplatform-patterns
280kMembantu agent Anda menggunakan pola Compose Multiplatform dan Jetpack Compose di proyek KMP, mencakup state, navigasi, theming, dan performa.
dart-flutter-patterns
280kMembuat agent Anda menulis kode untuk aplikasi Dart dan Flutter. Menggunakan pola modern agar aplikasi kokoh dan mudah dikelola.