Use Android Studio to detect memory leaks (leakcanary) _android

Source: Internet
Author: User
Tags memory usage

Memory leaks are the biggest headaches for Android developers. Perhaps a small memory leak could be the nest of a ruined sink. How can I detect a memory leak?
The Memory Control console (monitor) in Androidstudio provides a memory monitor. It allows us to easily view the performance and memory usage of the application, so that we can find the need to release the object, find the memory leak, and so on.

Familiar with the memory interface

With the log console open, there is a tag memory that we can use to analyze the memory used by the current program in this interface.

After you run the program you want to monitor (APP), open the Android Monitor Console window and see the memory console. Click the Enable button on the memory console and the memory console starts showing the memory usage of the program at run time. As shown in the figure above:

Features of Androidstudio Memory:

    • Start and close the Memory monitor button
    • Manually trigger the GC button
    • Dump Java heap button, click on Android Studio to start work, the success will automatically open the Hprof file.
    • The start (stop) Allocation tracking button clicks First, then sees memory recorder starts to rotate, then starts to do the corresponding operation on the app. Click again at the right time to end the record.

How to detect memory leaks

We click on the Dump Java heap button and the app will freeze live. About a few 10 seconds later,

The Hprof file is automatically opened when the dump succeeds.

If we want to know more about memory allocation, you can use allocation traker to see what the memory is occupied by. Click the STARG allocation Tracking button. Start assignment tracking, after some time, click the Stop allocation tracking to end the tracking position. When the trace is stopped, the. alloc file opens automatically.

When you want to see the source code of a method, the right key to select the method, click Jump to source on it.

Using Leakcanary

Leakcanary is a simple and crude tool for detecting memory leaks from the square company.

Leakcanary detects the application's memory recycle, and if it finds that a garbage object has not been recycled, it analyzes the current memory snapshot, which is the. hprof file that is used by the top mat, finds the reference chain of the object, and displays it on the page. The advantage of this plug-in is that you can see the memory leak directly on the phone side, can help us detect memory leaks.

Use:

Added in the Build.gradle file, different compilations use different references:

dependencies { 
  debugcompile ' com.squareup.leakcanary:leakcanary-android:1.3 '
  releasecompile ' com.squareup.leakcanary:leakcanary-android-no-op:1.3 '
}

Add Leakcanary.install (this) to the applied application OnCreate method, as follows:

public class Exampleapplication extends application {

 @Override public void OnCreate () {
  super.oncreate ();
  Leakcanary.install (this);
 }

When the application is running, Leakcanary will automatically analyze the current memory state, if the leak will be sent to the notification bar, click on the notification bar can jump to the specific leak analysis page.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.