Leakcanary Android and Java memory leak detection.

Source: Internet
Author: User

Start using

In build.gradle addition to the references, different compilations use different references:

 dependencies {   debugCompile ‘com.squareup.leakcanary:leakcanary-android:1.3‘   releaseCompile ‘com.squareup.leakcanary:leakcanary-android-no-op:1.3‘ }

In the Application :

public class ExampleApplication extends Application { @Override public void onCreate() { super.onCreate(); LeakCanary.install(this); }}


How to use

Use RefWatcher to monitor those objects that should be recycled.

RefWatcher refWatcher = {...};// 监控refWatcher.watch(schrodingerCat);

LeakCanary.install()will return a predefined activity that is RefWatcher also enabled ActivityRefWatcher for automatic monitoring after a call is Activity.onDestroy() released.

PublicClassExampleapplicationExtendsApplication{PublicStaticRefwatcherGetrefwatcher(ContextContext){ExampleapplicationApplication=(Exampleapplication)context. (); return application. Refwatcher} private refwatcher refwatcher @Override public void oncreate  () {super. Oncreate (); refwatcher = leakcanary. Install (this}             /span>                

Using RefWatcher monitoring Fragment:

Publicabstract class basefragment extends Fragment { @Override public void ondestroy< c10> () { Super.  OnDestroy(); refwatcher refwatcher = exampleapplication.  Getrefwatcher(getactivity()); refwatcher.  Watch(this); }}< /c14> 


Working mechanism
    1. RefWatcher.watch()Create a keyedweakreference to the object to be monitored.

    2. Then the background thread checks if the reference is cleared, and if not, the GC is called.

    3. If the reference is still not cleared, dump the heap memory into a file in the APP's file system .hprof .

    4. There is one in another process that HeapAnalyzerService HeapAnalyzer uses haha to parse this file.

    5. Thanks to the unique reference key, HeapAnalyzer locate KeyedWeakReference and locate the memory leak.

    6. HeapAnalyzerCalculates the shortest strong reference path to the GC roots and determines whether it is a leak. If so, establish a chain of references that leads to leaks.

    7. The reference chain is passed into the APP process and is displayed in the DisplayLeakService form of notifications.

Leakcanary Android and Java memory leak detection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.