Share
Embed
Transcript
13 slides
-
Android & Web Developer co founder of MonkeyPatch @EmmanuelVinas [email protected]
www.monkeypatch.io About me -
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” -
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; } } } -
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 ? -
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); } } -
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); } } -
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 -
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