java-memory-leaks
하는 일
이 스킬은 Android 앱이 해제해야 할 메모리를 계속 붙잡고 있는 이유를 찾도록 AI를 안내합니다. 테스트에서 누수 보고서를 보는 Android 개발자를 위한 스킬입니다.
사용하는 경우
- ✓테스트 로그에 LeakCanary 메모리 누수 보고서가 나타날 때.
- ✓닫힌 뒤에도 Android 화면이나 구성 요소가 메모리에 남아 있는 것 같을 때.
- ✓누수 추적을 읽고 비워야 할 객체를 찾아야 할 때.
스킬 링크
https://github.com/chromium/chromium/blob/main/agents/skills/java-memory-leaks/SKILL.md복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.
사용 방법
가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. 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란 무엇인가요?
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에이전트가 Android 코드에서 수동으로 작성한 Java와 C++ 간 변환 코드를 더 간단한 @JniType 주석으로 바꾸도록 돕습니다.
loadline
25k에이전트가 Android와 데스크톱 Chrome에서 웹 페이지가 얼마나 빨리 로드되는지 측정하는 LoadLine 벤치마크를 실행하도록 돕습니다.
비슷한 스킬
android-clean-architecture
280k에이전트가 Android 프로젝트를 효율적으로 구성하도록 돕습니다. 유지보수와 새 기능 개발이 쉽도록 코드를 구조화합니다.
compose-multiplatform-patterns
280k에이전트가 KMP 프로젝트에서 Compose Multiplatform과 Jetpack Compose 패턴을 쓰도록 돕습니다. 상태, 내비게이션, 테마, 성능을 다룹니다.
dart-flutter-patterns
280kDart와 Flutter 앱용 코드를 작성하도록 에이전트를 돕습니다. 견고하고 관리하기 쉬운 앱을 만드는 현대적인 패턴을 씁니다.