Android memory analysis tool

Source: Internet
Author: User

Android memory analysis tool
Garbage Collection (GC) for some memory knowledge of Android)

Garbage collection involves two processes:

The determination phase, that is, to determine which objects can be recycled. The collection phase refers to a specific collection policy.

There are two ways to determine the phase

Reference count: each time an object has one more reference count plus one. If one reference count is less than one, the object can be recycled if the count is 0. However, the reference count has a disadvantage, and it cannot be used to judge the case of loop applications. Therefore, the following method is used for root search to traverse the search from some root objects (GCRoot, if an object cannot be searched, the object can be recycled.
It can be used as a GCRoot object:
1. Some Virtual Machine stack objects; 2. Class static property objects in the method area; 3. Constant objects in the method area; 4. Reference objects of JNI in the Native Stack

There are four main methods in the collection phase

Mark clearing: the simplest algorithm. Mark objects are cleared directly, which is fast but inefficient. The memory fragment replication algorithm uses half of the available memory each time, during collection, the available objects are copied to the other half of the memory, and the remaining parts are collected and sorted to the end of the memory area, the memory area is divided into the young and old ages by the object lifecycle. Different regions use different collection algorithms. Dalvik and ART

Before Android, The Dalvik virtual machine was used, and then the ART virtual machine was used. Below are some comparisons:

Dalvik converts bytecode to machine code during runtime, and ART converts it to machine code during installation. In this way, the installed application occupies more space, but the conversion time is less during runtime, therefore, faster running of ART provides better performance of garbage collection. During garbage collection, the number of pauses of the program is reduced from two times (analysis and cleanup) to one. When the program is paused, the garbage collection process is performed in parallel. The recently allocated objects with short lifecycles are recycled. The Garbage Collector spends less time on Android Memory analysis tools. # other threads need to be paused for the Memory Monitor GC operation, therefore, frequent GC may affect the UI thread in a short time, resulting in frequent GC in two situations: a large number of objects are created and released immediately in a short time, for example, when the memory area of the object-Young Generation created in the onDraw method of the View reaches the threshold and the remaining space is insufficient, frequent GC is also triggered. Android Studio provides Memory Monitor to display the Memory usage during application running in real time. The blue part below is the Memory occupied currently, The gray section above shows recycled memory. If you see a spike in the figure, that is, the rapidly allocated memory is recycled, that is, the memory jitter occurs. Here is where optimization is needed.

 

 

Allocation Tracking

Allocation Tracking is a memory tool provided in DDMS to Display memory Allocation for a period of time.

Select the process name to be tracked, click Start Tracking to Start Tracking, and click Get Allocations to display the newly allocated objects in this operation, click a specific object to see which method is assigned this object.

Heap Tool and MAT

Heap Tool can view the current memory Snapshot

From the data, we can see the current memory usage and recovery status. The data here will be updated every time garbage collection is made, because the memory data will be continuously retrieved for refresh display, so the application operation will be stuck at this time.
The Heap Tool provides a general memory condition, and the chart display content is relatively simple. If you want to analyze it, you 'd better generate a. hprof file and use the MAT Tool for analysis.

There are many introductions on the use of the MAT tool. google has written an official introduction to the use of the MAT tool http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html. A Chinese Blog is recommended,
Http://blog.csdn.net/guolin_blog/article/details/42242433.

Generally, the MAT tool is used to analyze the memory because the application has experienced a memory leak. You need to analyze the potential leakage and then use the MAT tool for verification. Recently, Square open-source LeakCanary, a memory leak detection project, greatly simplifies this process. It can be said that it is the ultimate tool for Android Memory Leak Detection.

LeakCanary

A memory leak detection library for Android and Java.
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.

Usage:
Add

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

In the Application onCreate method of the Application, addLeakCanary.install(this), As shown below

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

 

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.