Use the new Android Studio version to detect memory leakage and Performance

Source: Internet
Author: User

Use the new Android Studio version to detect memory leakage and Performance

Memory leakage is the biggest headache for Android Developers. A small memory leak may all be an ant hole broken down by the treasure of thousands of miles.
How can we detect memory leaks? There are a lot of online tutorials, but many of them are detected using Eclipse. In fact, Android Studio after version 1.3 is very convenient to detect memory. If you use the MAT tool and LeakCanary plug-in, everything becomes so easy.

Familiar with the Android Studio Interface

To do well, you must first sharpen your tools. Let's familiarize ourselves with the Android Studio interface.

For general analysis of Memory leakage, first run the program and open the log console, there is a label MemZ memory? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcnkgLM7Sw8e/pushed/2LCDSu8S/pushed/4L/pushed + pushed/pushed + NPyvs3Kx7/pushed + tbGyse21MO/pushed + listen + G99L3T18W4 + weight + 31 iyxyMjntO + weight + FPT00oxNq05tLns/weight + weight/rD3MfQz + weight/weight + bXEyta7 + s/weight = "How do we detect memory leaks?"> How do we detect memory leaks?

First, you need to understand the concept that memory leakage refers to the memory that should be recycled and also resident in the memory.
Generally, a page on a high-density mobile phone consumes about 20 mb of memory. If you find that the program memory does not decrease, a serious memory leakage may occur.
We can go to the interface repeatedly, click the dump java heap button, and then Android Studio starts to work. The figure below is

After the dump succeeds, the hprof file is automatically opened. The file is named after Snapshot + time.

Through the Android Studio built-in interface, check the memory leakage is not very intelligent, we can use third-party tools, the common tool is MAT, http://eclipse.org/mat/downloads.php, here we need to download the independent version of MAT. this is the interface opened by MAT at the beginning. We need to remind you that MAT does not accurately tell us where memory leakage occurs, but will provide a lot of data and clues, we need to analyze the data to determine whether a memory leak has actually occurred.

Next, we need to use MAT to open the memory analysis file. The preceding section describes how to use Android Studio to generate the hprof file. This file is located in the Captrues directory of Android Studio.

Note: This file cannot be handed over directly to MAT. MAT is unrecognized. We need to right-click this file and convert it to MAT for recognition.

Then use MAT to Open the exported hprof (File-> Open heap dump). MAT will help us analyze the cause of Memory leakage.

LeakCanary

The above section describes how to use MAT to detect memory leaks and introduce LeakCanary to you.
Project address: https://github.com/square/leakcanary

LeakCanary checks the memory Recycle of the application. If spam objects are not recycled, LeakCanary analyzes the current memory snapshot, which is used by the above MAT. hprof file, locate the object reference chain, and display it on the page. The advantage of this plug-in is that you can directly view the memory leakage on the mobile phone end, which can help us detect memory leakage

Usage:
In the build. gradle file, add different references for different compilation:

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

Add LeakCanary. install (this) in the Application onCreate method of the Application, as shown below:

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

After the application runs, LeakCanary automatically analyzes the current memory status. If leakage is detected, it is sent to the notification bar. Click the notification bar to go to the specific leak Analysis page.
Tips: based on the results currently used, the vast majority of leaks are caused by the use of Singleton mode to hold the reference of the Activity. For example, if context is introduced or the Activity is set as the listener, therefore, when using the singleton mode, pay special attention to the fact that some custom listener Activity References are left blank at the end of the Activity lifecycle.
For more analysis of LeakCanary, see the introduction of the project home page, and here http://www.liaohuqiu.net/cn/posts/leak-canary-read-me/

Trace memory allocation

If you want to know more details about memory Allocation, you can use Allocation Traker to check the memory usage.
Easy to use:

Click tracking, and click stop tracking. The. alloc file is automatically opened after the tracing is stopped. The page is displayed as follows:

When you want to view the source code of a method, right-click the method selected and click Jump to source.

Query Method execution time

Android Studio is becoming more and more powerful. We can use AS to observe various performances, such:

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

Click Start Method Tracking, and click again after a period of time. The trace file is automatically opened,

Non-exclusive time:The CPU time used by a function, including the CPU time when other functions are called internally.
Exclusive time:A function consumes CPU time, but does not include the CPU time used to call other functions internally.

How can we identify possible problems?

You can view the call times and exclusive time of a method. The method is usually as follows:

If the number of method calls is small, but each call takes a long time, a problem may occur. Functions that consume less time but are called frequently may also have problems. Summary

We have introduced a number of tools that use Android Studio to check program performance. The tool will always be a helper, so do not delay the tool for a long time. If you have any questions, you are welcome to correct them.

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.