java-memory-leaks
ما الذي تفعله
توجه هذه المهارة الذكاء الاصطناعي لمعرفة سبب احتفاظ تطبيق Android بذاكرة كان ينبغي تحريرها. وهي مخصصة لمطوري Android الذين يرون تقارير تسريب في اختباراتهم.
متى تستخدمها
- ✓عندما يُظهر سجل اختبار تقرير تسريب ذاكرة من LeakCanary.
- ✓عندما يبدو أن شاشة Android أو مكوّنًا يبقى في الذاكرة بعد إغلاقه.
- ✓عندما تحتاج إلى قراءة مسار التسريب وإيجاد الكائن الذي يجب تحريره.
رابط المهارة
https://github.com/chromium/chromium/blob/main/agents/skills/java-memory-leaks/SKILL.mdانسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.
طريقة الاستخدام
أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.
رسالة جاهزة للصق
لـ Claude Code أو Codex أو Cursor: شغّله داخل مجلد مشروعك وستُثبَّت المهارة.
نزّل ملف SKILL.md وضعه في مجلد مهارات وكيلك.
مثال على ما تطلبه من الذكاء الاصطناعي
“يُظهر اختبار Android لدي تسريبًا من LeakCanary. هل يمكنك قراءة المسار ومساعدتي في إصلاحه؟”
5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.
كيف تعمل
يتأكد الذكاء الاصطناعي من التسريب بتشغيل الاختبار، ثم يقرأ مسار التسريب من الجذر إلى الكائن المتسرب، ويجد المرجع الذي يجب مسحه، ويقترح إصلاحًا.
المحتوى التقني (ملف 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 لاستخدام هذه المهارة؟
نعم، فالمهارة تعمل مع شيفرة Java على Android. والذكاء الاصطناعي هو من يقرأ المسار ويعدّل الشيفرة.
التفاصيل
الوصف الأصلي من المؤلف
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
التعليقات
- …
المزيد من المهارات من هذا المستودع
fuzzing
25kيساعد وكيلك على كتابة اختبارات فازينغ تمرر مدخلات عشوائية كثيرة إلى الشيفرة لاكتشاف الأعطال والأخطاء في شيفرة C++.
gn-deps-debugging
25kيساعد وكيلك على إصلاح أخطاء البناء الناتجة عن تبعيات ناقصة أو خاطئة في ملفات بناء GN، دون إضافة تبعيات واسعة وغير لازمة.
jni-type-conversion
25kيساعد وكيلك على استبدال شيفرة التحويل اليدوي بين Java وC++ بتعليقات @JniType أبسط في شيفرة Android.
loadline
25kيساعد وكيلك على تشغيل اختبارات أداء LoadLine التي تقيس سرعة تحميل صفحات الويب على Android وChrome للحاسوب.
مهارات مشابهة
android-clean-architecture
280kينظّم وكيلك مشاريع أندرويد بكفاءة، ويبني الشيفرة بطريقة تجعل الصيانة وإضافة الميزات الجديدة أسهل.
compose-multiplatform-patterns
280kيساعد وكيلك على استعمال أنماط Compose Multiplatform وJetpack Compose في مشاريع KMP، وتشمل الحالة والتنقل والتصميم والأداء.
dart-flutter-patterns
280kيساعد وكيلك على كتابة شيفرة تطبيقات Dart وFlutter، باستعمال أنماط حديثة لبناء تطبيقات متينة وسهلة الإدارة.