Android uses Leakcanary to detect memory leaks

Source: Internet
Author: User
<span id="Label3"></p><p><p>Leakcanary is the Android and Java memory leak detection framework, an open source library for Square company, project address Leakcanary.</p></p><p><p>Do you frequently encounter memory leaks in Android development and have no Solution. Maybe one day you accidentally write a line of code that causes a memory Leak. It is possible to look at the memory leaks caused by these problems and the memory leaks caused by the Android development coding specification, while leakcanary is straightforward enough to detect the memory leaks and show it to Us. Before using it, let's write an example.</p></p><p><p>Local broadcast, in the development of a certain application, there is now a requirement to use local broadcast to achieve, that is, by sending an exit program of the local broadcast, all the activity received after the exit, it is obviously necessary a basic activity, the other activity inherits it. For convenience, We only use one activity Here.</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">appcompatactivity</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>String Action_exit_app =<span class="hljs-string"><span class="hljs-string">"cn.edu.zafu.leakcanary.exit"</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>Localbroadcastmanager mlocalbroadcatmanager;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Broadcastreceiver Mexitreceiver =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Broadcastreceiver () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnReceive</span></span>(context context, Intent Intent) {String action = intent.getaction ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(action.equals (action_exit_app)) {LOG.D (<span class="hljs-string"><span class="hljs-string">"TAG"</span></span>,<span class="hljs-string"><span class="hljs-string">"exit from broadcast"</span></span>); Finish (); } } };<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Init (); }<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Init</span></span>() {intentfilter filter =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intentfilter (); Filter.addaction (action_exit_app); Filter.addcategory (intent.category_default); Getlocalbroadcastmanager (). registerreceiver (mexitreceiver, filter); }<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Localbroadcastmanager<span class="hljs-title"><span class="hljs-title">Getlocalbroadcastmanager</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(mlocalbroadcatmanager = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {mlocalbroadcatmanager = Localbroadcastmanager.getinstance (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>mlocalbroadcatmanager; }}</code></pre><p><p>At first glance, is not the feeling of writing is right ah, then you are not careful enough, this is still a small amount of code, for the long-accumulated code in the project, memory leaks may be everywhere. We use Leakcanary to test our code to see exactly where the memory leak has occurred and how to resolve it.</p></p><p><p>The use of the method is also very simple, first join the dependency</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs bash"><span class="hljs-string">‘com.squareup.leakcanary:leakcanary-android:1.3.1‘</span><span class="hljs-string">‘com.squareup.leakcanary:leakcanary-android-no-op:1.3.1‘</span></code></pre></pre><p><p>From the dependence can also be seen to be fishy.</p></p><p><p>Then install it in the applictaion of our program, and of course, don't forget to register the application in the manifest File.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword">public</span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Application</span> {</span> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onCreate</span>() { <span class="hljs-keyword">super</span>.onCreate(); LeakCanary.install(<span class="hljs-keyword">this</span>); }}</code></pre></pre><p><p>I said it's so simple you'll believe it, ok, Let's install it on the phone and see. After the installation is complete, run the software, open and exit the software, when you find a leaks icon on the Desktop.</p></p><p><p></p></p><p><p>When you open it, the notification bar will have a notification informing you that a memory leak has Occurred.</p></p><p><p></p></p><p><p>Then in the software you will see the memory leak tracking Information.</p></p><p><p></p></p><p><p>Click Delete below to delete this Message.</p></p><p><p>Look carefully, It turns out that our <strong>Mlocalbroadcatmanager</strong> has leaked, registered the local broadcast, passed in this, <strong></strong>The system kept this reference, when we quit activity, This reference still points to our activity, which causes the activity to fail to Recycle. So how to solve, since the withdrawal of the time also hold a reference, then we cancel the registration of this broadcast this reference is not gone, rewrite the OnDestroy method, to Unregister.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-annotation">@Override</span><span class="hljs-keyword">protected</span><span class="hljs-keyword">void</span><span class="hljs-title">onDestroy</span>() { <span class="hljs-keyword">super</span>.onDestroy(); getLocalBroadcastManager().unregisterReceiver(mExitReceiver);}</code></pre></pre><p><p>re-run, gee, you find that the memory is no longer leaking. The software is no longer prompted for memory leak tracking Information.</p></p><p><p></p></p><p><p>It is that simple, if you want to learn more about the use of methods, such as detecting fragment leaks. can refer to the official example, and the memory leak tracking information can be uploaded to the server, more content, reference leakcanary</p></p><p><p>SOURCE download</p></p> <ul> <ul> <li>http://download.csdn.net/detail/sbsujjbcy/9048449</li> </ul> </ul> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android uses Leakcanary to detect memory leaks</p></p></span>
Related Article

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.