SlideShare ist ein Scribd-Unternehmen logo
1 von 14
leakcanary tool
TimChen (陳孝庭)
e-mail: fatfattim@gmail.com
Motivation
● Testing
○ Automation - Espresso
○ How to monitor Memory leak by passively ?
● Why leakcanary ?
○ Open source
○ Great develop resource
■ okhttp
■ picasso
What is leakcanary ?
● A memory leak detection tool
● Avoid OOM (Out Of Memory)
○ A memory leak detection library for Android and
Java
○ java.lang.OutOfMemoryError
● How to integrate leakcanary into Android
project ?
○ here
leakcanary
● Automatically detects if an activity is leaking
after Activity.onDestroy() has been called.
● ActivityRefWatcher
○ Application.ActivityLifecycleCallbacks
■ onActivityDestroyed
leakcanary
● Detection flow
Watch Check Dump & Parse
If get leak information, it
will send notification and
show message in leak
application
leakcanary-Watch
● RefWatcher Class : watch method
a. Give weak wrapper for watched object
public void watch(Object watchedReference, String referenceName) {
if (this == DISABLED) {
return;
}
checkNotNull(watchedReference, "watchedReference");
checkNotNull(referenceName, "referenceName");
final long watchStartNanoTime = System.nanoTime();
String key = UUID.randomUUID().toString();
retainedKeys.add(key);
final KeyedWeakReference reference =
new KeyedWeakReference(watchedReference, key, referenceName, queue);
ensureGoneAsync(watchStartNanoTime, reference);
}
leakcanary-Check
● RefWatcher Class : ensureGone method
a. Run in background thread
b. Do garbage collection
c. Check watched reference is gone or not
● If not release object well
a. Go to heap dump state
leakcanary-Dump
● AndroidHeapDumper : dumpHeap method
a. Create storage file from leak directory
■ /sdcard/Download/leakcanary-${packagename}
b. Dump by Debug.dumpHprofData API
leakcanary-Parse
● HeapAnalyzerService is started in a
separate process and HeapAnalyzer parses
the heap dump using HAHA.
● Send notification and show message in
Exclude SDK leak
● Yes. There are a number of known memory leaks that
have been fixed over time in AOSP as well as in
manufacturer implementations. When such a leak
occurs, there is little you can do as an app developer to
fix it. For that reason, LeakCanary has a built-in list of
known Android leaks to ignore:
AndroidExcludedRefs.java.
Demo
● Debug v.s Production
● Leak in Activity
● Leak in some object
Q & A
● How to exclude leak info you want to ignore
?
○ Ignoring specific references
● How do I fix a memory leak?
○ here
Thank you!

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (6)

Infrastructure as Code
Infrastructure as CodeInfrastructure as Code
Infrastructure as Code
 
PhoneGap
PhoneGapPhoneGap
PhoneGap
 
Will it blend? Java agents and OSGi
Will it blend? Java agents and OSGiWill it blend? Java agents and OSGi
Will it blend? Java agents and OSGi
 
What can possibly go wrong if i dont e2 e test my packages?
What can possibly go wrong if i dont e2 e test my packages?What can possibly go wrong if i dont e2 e test my packages?
What can possibly go wrong if i dont e2 e test my packages?
 
TDD CrashCourse Part1: Testing
TDD CrashCourse Part1: TestingTDD CrashCourse Part1: Testing
TDD CrashCourse Part1: Testing
 
Firmware Extraction & Fuzzing - Jatan Raval
Firmware Extraction & Fuzzing - Jatan RavalFirmware Extraction & Fuzzing - Jatan Raval
Firmware Extraction & Fuzzing - Jatan Raval
 

Ähnlich wie Leakcanary tool

A small leak can sink a great ship !!
A small leak can sink a great ship !!A small leak can sink a great ship !!
A small leak can sink a great ship !!
Ayushi Gupta
 

Ähnlich wie Leakcanary tool (20)

Xamarin.android memory management gotchas
Xamarin.android memory management gotchasXamarin.android memory management gotchas
Xamarin.android memory management gotchas
 
A small leak can sink a great ship !!
A small leak can sink a great ship !!A small leak can sink a great ship !!
A small leak can sink a great ship !!
 
Effective memory management
Effective memory managementEffective memory management
Effective memory management
 
Effective memory management
Effective memory managementEffective memory management
Effective memory management
 
10 ways to improve your Android app performance
10 ways to improve your Android app performance10 ways to improve your Android app performance
10 ways to improve your Android app performance
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
 
Android reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skypeAndroid reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skype
 
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...
 
Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...
Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...
Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
 
Fuzzing - Part 2
Fuzzing - Part 2Fuzzing - Part 2
Fuzzing - Part 2
 
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017
 
Monitoring Big Data Systems Done "The Simple Way" - Demi Ben-Ari - Codemotion...
Monitoring Big Data Systems Done "The Simple Way" - Demi Ben-Ari - Codemotion...Monitoring Big Data Systems Done "The Simple Way" - Demi Ben-Ari - Codemotion...
Monitoring Big Data Systems Done "The Simple Way" - Demi Ben-Ari - Codemotion...
 
Monitoring Big Data Systems "Done the simple way" - Demi Ben-Ari - Codemotion...
Monitoring Big Data Systems "Done the simple way" - Demi Ben-Ari - Codemotion...Monitoring Big Data Systems "Done the simple way" - Demi Ben-Ari - Codemotion...
Monitoring Big Data Systems "Done the simple way" - Demi Ben-Ari - Codemotion...
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
 
Thinking DevOps in the era of the Cloud - Demi Ben-Ari
Thinking DevOps in the era of the Cloud - Demi Ben-AriThinking DevOps in the era of the Cloud - Demi Ben-Ari
Thinking DevOps in the era of the Cloud - Demi Ben-Ari
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
 

Leakcanary tool

  • 2. Motivation ● Testing ○ Automation - Espresso ○ How to monitor Memory leak by passively ? ● Why leakcanary ? ○ Open source ○ Great develop resource ■ okhttp ■ picasso
  • 3. What is leakcanary ? ● A memory leak detection tool ● Avoid OOM (Out Of Memory) ○ A memory leak detection library for Android and Java ○ java.lang.OutOfMemoryError ● How to integrate leakcanary into Android project ? ○ here
  • 4.
  • 5. leakcanary ● Automatically detects if an activity is leaking after Activity.onDestroy() has been called. ● ActivityRefWatcher ○ Application.ActivityLifecycleCallbacks ■ onActivityDestroyed
  • 6. leakcanary ● Detection flow Watch Check Dump & Parse If get leak information, it will send notification and show message in leak application
  • 7. leakcanary-Watch ● RefWatcher Class : watch method a. Give weak wrapper for watched object public void watch(Object watchedReference, String referenceName) { if (this == DISABLED) { return; } checkNotNull(watchedReference, "watchedReference"); checkNotNull(referenceName, "referenceName"); final long watchStartNanoTime = System.nanoTime(); String key = UUID.randomUUID().toString(); retainedKeys.add(key); final KeyedWeakReference reference = new KeyedWeakReference(watchedReference, key, referenceName, queue); ensureGoneAsync(watchStartNanoTime, reference); }
  • 8. leakcanary-Check ● RefWatcher Class : ensureGone method a. Run in background thread b. Do garbage collection c. Check watched reference is gone or not ● If not release object well a. Go to heap dump state
  • 9. leakcanary-Dump ● AndroidHeapDumper : dumpHeap method a. Create storage file from leak directory ■ /sdcard/Download/leakcanary-${packagename} b. Dump by Debug.dumpHprofData API
  • 10. leakcanary-Parse ● HeapAnalyzerService is started in a separate process and HeapAnalyzer parses the heap dump using HAHA. ● Send notification and show message in
  • 11. Exclude SDK leak ● Yes. There are a number of known memory leaks that have been fixed over time in AOSP as well as in manufacturer implementations. When such a leak occurs, there is little you can do as an app developer to fix it. For that reason, LeakCanary has a built-in list of known Android leaks to ignore: AndroidExcludedRefs.java.
  • 12. Demo ● Debug v.s Production ● Leak in Activity ● Leak in some object
  • 13. Q & A ● How to exclude leak info you want to ignore ? ○ Ignoring specific references ● How do I fix a memory leak? ○ here