Android Studio detects memory leaks and performance

Source: Internet
Author: User

Han Mengfei sha yue31313 Han Yafei Han_meng_fei_sha [email protected]

The first thing you need to understand is that memory leaks mean that the memory that should be recycled still resides in memory.
In general, high-density mobile phones, a page will probably consume 20M of memory, if you find out the interface, the program memory is slow, there may be a serious memory leak.
We can go through the interface again and then click the Dump Java heap button, and then Android Studio will start working, and the graph below is the dump

When dump succeeds, the Hprof file is automatically opened, and the file is named with snapshot+ time.

With Android studio's own interface, it's not smart to see a memory leak, we can use third-party tools, the common tool is mat, http://eclipse.org/mat/downloads.php, here we need to download the standalone version of Mat . It's the beginning of the mat opening, and here's a reminder that Mat doesn't tell us exactly where the memory leak is, but rather provides a whole bunch of data and clues that we need to analyze to determine if a memory leak really happened.

Next we need to use the mat to open the memory analysis of the file, described above for the use of Android Studio generated hprof files, this file in the Android studio in the Captrues this directory, you can find

Note that this file cannot be directly handed to Mat, Mat is not aware of anything else, we need to right click on this file and convert it to mat recognition.

Then using the mat to open the exported hprof (File->open heap dump) Mat will help us analyze the cause of the memory leak.

Leakcanary

The above describes the mat to detect memory leaks, and then to introduce you to leakcanary.
Project Address: Https://github.com/square/leakcanary

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

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

?
1234 <code class=" hljs matlab">dependencies {     debugCompile ‘com.squareup.leakcanary:leakcanary-android:1.3‘    releaseCompile ‘com.squareup.leakcanary:leakcanary-android-no-op:1.3‘}</code>

Add Leakcanary.install (this) to the application's application OnCreate method, as follows

?
1234567 <code class=" hljs java">public class ExampleApplication extends Application     @Override    public void onCreate() {        super.onCreate();         LeakCanary.install(this);     } }</code>

After the application is run, Leakcanary will automatically analyze the current state of memory, if a leak is detected will be sent to the notification bar, click on the notification bar to jump to the specific leak analysis page.
Tips: As far as the results are currently used, most leaks are due to the use of a singleton mode to hold the activity reference, such as the context or activity as a listener set into, so in the use of the singleton mode of special attention, There are also empty activity references for some custom listeners at the end of the activity life cycle.
More analysis of Leakcanary can be seen in the introduction of the project homepage, and here http://www.liaohuqiu.net/cn/posts/leak-canary-read-me/

Track Memory Allocations

If we want to know more about memory allocations, you can use allocation traker to see what the memory is taking up.
The usage is simple:

Click to track, then click Stop Tracking, stop tracking after the. alloc file will open automatically, open after the interface is as follows:

When you want to view the source code of a method, right-click on the method, tap jump to Source

How long the Query method executes

Android Studio is becoming more and more powerful, and we can use as to observe a variety of performance, such as:

If we want to observe the time of the method execution, we need to come to the CPU interface

Click on Start Method Tracking, after a period of time and then click again, trace files are automatically opened,

Inclusive time: The CPU time that a function consumes, including the CPU time that the other functions are called internally.
Exclusive time: a function consumes CPU time, but does not contain the CPU time that is consumed by internal calls to other functions.

How do we judge a possible problem?

The method is usually judged by the number of calls and exclusive times of the methods:

If the number of method calls is not many, but each call will take a long time of the function, there may be problems. A function that is called very frequently can be problematic if it takes a long time.

Android Studio detects memory leaks and performance

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.