Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Leak Canary presentation - TAUG - 24-02-2016

Leak Canary presentation - TAUG - 24-02-2016

Avatar for Emmanuel Vinas

Emmanuel Vinas

February 25, 2016
Tweet

More Decks by Emmanuel Vinas

Other Decks in Programming

Transcript

  1. Memory leak “Memory leak is existence of objects in the

    memory, that are not needed anymore according to the application logic, but still retain memory and cannot be collected because they are referenced from other live objects, due to a bug in application itself”
  2. Memory leak : a basic example public class MemoryLeakActivity extends

    Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); View button = findViewById(R.id.async_task); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new SampleTask ().execute(); } }); } private class SampleTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { // Some slow work in background SystemClock.sleep(20000); return null; } } }
  3.  Why leak canary name ?  Launched in May

    2015 by square (Pierre yves Ricau).  Currently in version 1.3  Detect memory leak during runtime Leak Canary. what’s that ?
  4. Leak Canary : Configuration dependencies { debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1' releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'

    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1' } public class ExampleApplication extends Application { @Override public void onCreate() { super.onCreate(); LeakCanary.install(this); } }
  5. Leak Canary in Action public abstract class BaseFragment extends Fragment

    { @Override public void onDestroy() { super.onDestroy(); RefWatcher refWatcher = ExampleApplication.getRefWatcher(getActivity()); refWatcher.watch(this); } } public class ExampleApplication extends Application { public static RefWatcher getRefWatcher(Context context) { ExampleApplication application = (ExampleApplication) context.getApplicationContext(); return application.refWatcher; } private RefWatcher refWatcher; @Override public void onCreate() { super.onCreate(); refWatcher = LeakCanary.install(this); } }
  6.  RefWatcher.watch() creates a reference to the watched object. 

    Later, in a background thread, it checks if the reference has been cleared and if not it triggers a GC.  If the reference is still not cleared, it then dumps the heap into a .hprof file stored on the app file system.  HeapAnalyzerService is started in a separate process and HeapAnalyzer parses the heap dump using HAHA.  HeapAnalyzer finds the KeyedWeakReference in the heap dump thanks to a unique reference key and locates the leaking reference.  HeapAnalyzer computes the shortest strong reference path to the GC Roots to determine if there is a leak, and then builds the chain of references causing the leak.  The result is passed back to DisplayLeakService in the app process, and the leak notification is shown. How does this works
  7.  Easy to send Memory leaks to crash report service

    like crashlytics…  Run with instrument test, CI compliant  Ignore specific reference to avoid system ML  Not watching specific activity classes Other nice feature