Detect memory leaks and performance with the new version of Android studio

Source: Internet
Author: User

Http://www.jianshu.com/p/216b03c22bb8

Memory leaks are the biggest headache for Android developers. Perhaps a small memory leak, could be destroyed in the dikes nest. How can I detect a memory leak? There are many online tutorials, but many are using eclipse detection, in fact, after the 1.3 version of Android Studio detection memory is very convenient, if combined with the Mat tool, Leakcanary plug-ins, everything becomes so easy.

Familiar with Android Studio interface 工欲善其事, its prerequisite.

Let's start with the Android studio interface.


Memory Analysis Interface

General analysis of memory leaks, first run the program, open the log console, there is a tag memory, we can analyze the current application in this interface to use the situation, at a glance, we no longer need to logcat in the search for memory in the log.

The blue area in the graph is the memory used by the program, and the gray area is the free memory.

Of course, the Android memory allocation mechanism is gradually increased for each application, such as your program currently using 30M of memory, the system may assign you 40M, the current is 10M idle, if the program uses 50M, the system will immediately add a portion of the current program, such as reached the 80M, Your free memory is now 30M. Of course, if the system can no longer allocate additional memory to you, the program will naturally be oom (memory overflow). Each application can apply the highest memory and phone closely related, such as I am currently using the Huawei Mate7, the limit is probably 200M, is relatively high, the general 128M is the limit, and even some phones only poor 16M or 32M, such a cell phone relative to the probability of memory overflow is very large.

How do we detect a memory leak?

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


is dump

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


Memory Analysis Results

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.


Mat Main interface

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


HPROF Catalogue]

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.


Export the standard hprof

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


Open the standard hprof
Automatic analysis of causes of memory leaks leakcanary

The above describes the mat to detect memory leaks, and then to introduce you to leakcanary.
Project Address: Https://github.com/square/leakcanaryLeakCanary
Detects the memory recycling of the application, and if it finds that a garbage object is not being 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.


View memory leaks on the phone. png

Use:
Add 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 application's application OnCreate method, as follows:

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

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 on 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 memory allocations

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


Track Memory structure display


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:


Detection Performance Interface


The Android studio2.1 interface has changed, with no significant difference in functionality:


New version Interface

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


Time-consuming tracking methods

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


Results

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:

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

Here are some of the tools used by Android Studio to check the performance of the program, the tool is always auxiliary, do not because the tool delay too long. If there are any problems, please correct them.

We've recently opened a public account.


Public account: Likedev Android Utility technology? Copyright belongs to author all report article Yu Lianlin 520WCF

Wrote 62500 words, was paid attention by 1268 people, gained 2,495 likes

Public account: LIKEDEVQQ Learning Group: 488929846Android ebook: http://book.520wcf.com video address: Http://youku.520wcf.comCSDN Blog Address:/http Blog.csdn.net/yulianlin

More exciting content, follow the public account Likedev, if the work is helpful to you, feel free to reward

More shares 35 reviews only see the author by the Order of preference sort by time reversed Mrwangyong6 building · 2016.01.02 19:49

Good thing, thanks for hugging.

1 likes reply to the 9172 floor of the herd · 2015.12.27 05:40

Not bad

Praise Reply

Yu Lianlin 520WCF: @ herd 917 Thanks for supporting

2015.12.29 09:12 reply.

And I'm still floating fist oh: @ Yu Lianlin 520wcf really good

2016.06.01 11:33 reply Add a new comment MrFu3 building · 2015.12.27 17:26

It's great.

Praise Reply

Yu Lianlin 520WCF: @MrFu swim.

2015.12.29 09:12 reply Add a new comment Lo_android4 building · 2015.12.29 20:36

Good stuff.

Praise Reply

Yu Lianlin 520WCF: @lo_android Thank you for your support

2015.12.30 07:42 reply Add a new comment diligence small Xizi 5 Floor · 2016.01.01 00:33

Oh, great

Praise reply Small Toad Zhi 7 Lou · 2016.04.09 11:11

Good article.

Praise Reply ChrisYuu8 Building · 2016.04.15 14:42

How to locate the memory leak according to the depth?

Like to reply to the lonely leaves 9 floor · 2016.04.16 21:39

Very good

Praise reply Huang Gai suddenly 10 floor · 2016.04.28 13:41

Collection of

Praise Reply QQ0000111 Building · 2016.04.28 13:49

When I was worrying, I found this article in the Great God.

Praise Reply SwitchCase12 Building · 2016.04.28 17:40

Support

Praise Reply

Yu Lianlin 520WCF: @SwitchCase Thank you

2016.04.28 17:54 reply Add a new comment mi-ryung MeYoung13 building · 2016.04.30 23:56

Study the

14 Floor of a season · 2016.05.10 22:49

Must praise.

Detect memory leaks and performance with the new version of Android studio

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.