java-memory-leaks
यह क्या करती है
यह स्किल आपके AI को यह खोजने में मार्गदर्शन देती है कि Android ऐप वह मेमोरी क्यों रखता है जो उसे छोड़ देनी चाहिए। यह उन Android डेवलपर्स के लिए है जो अपने टेस्ट में लीक रिपोर्ट देखते हैं।
कब इस्तेमाल करें
- ✓जब टेस्ट लॉग में LeakCanary मेमोरी लीक रिपोर्ट दिखे।
- ✓जब कोई Android स्क्रीन या कंपोनेंट बंद होने के बाद भी मेमोरी में बना रहता है।
- ✓जब आपको लीक ट्रेस पढ़कर वह ऑब्जेक्ट खोजना है जिसे साफ़ होना चाहिए।
Skill का लिंक
https://github.com/chromium/chromium/blob/main/agents/skills/java-memory-leaks/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“मेरे Android टेस्ट में LeakCanary लीक दिख रहा है। क्या आप ट्रेस पढ़कर इसे ठीक करने में मदद कर सकते हैं?”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
AI टेस्ट चलाकर लीक की पुष्टि करता है, root से लीक करने वाले ऑब्जेक्ट तक का लीक ट्रेस पढ़ता है, वह reference खोजता है जिसे साफ़ होना चाहिए, और सुधार सुझाता है।
तकनीकी सामग्री (मूल 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
- अपडेट की गई
- 2/11/2026
कमेंट
- …
इस repository की और skills
fuzzing
25kयह आपके एजेंट को फ़ज़ टेस्ट लिखने में मदद करती है, जो कोड में बहुत से यादृच्छिक इनपुट डालकर C++ कोड में क्रैश और बग खोजते हैं।
gn-deps-debugging
25kयह आपके एजेंट को GN build फ़ाइलों में गायब या गलत निर्भरताओं से होने वाली build त्रुटियाँ ठीक करने में मदद करती है, बिना बड़ी और अनावश्यक निर्भरताएँ जोड़े।
jni-type-conversion
25kयह आपके एजेंट को Android कोड में हाथ से लिखे Java-से-C++ रूपांतरण कोड की जगह सरल @JniType annotations लगाने में मदद करती है।
loadline
25kयह आपके एजेंट को LoadLine बेंचमार्क चलाने में मदद करती है, जो मापते हैं कि Android और डेस्कटॉप Chrome पर वेब पेज कितनी तेज़ी से लोड होते हैं।
मिलती-जुलती skills
android-clean-architecture
280kयह आपके एजेंट को Android projects को व्यवस्थित तरीके से संभालने देता है। यह code को ऐसा ढांचा देता है कि उसकी देखभाल और नए features जोड़ना आसान हो।
compose-multiplatform-patterns
280kKMP projects में Compose Multiplatform और Jetpack Compose patterns इस्तेमाल करने में आपके एजेंट की मदद करता है। इसमें state, navigation, theming और performance शामिल हैं।
dart-flutter-patterns
280kयह आपके एजेंट को Dart और Flutter apps के लिए code लिखने देता है। यह आधुनिक patterns इस्तेमाल करके मज़बूत और आसानी से संभलने वाले apps बनाता है।