Android Memory Analysis Tool

Source: Internet
Author: User

Some memory knowledge of Android garbage collection (GC)

Garbage collection consists of two processes:

    • The decision stage, which is to determine which objects can be recycled,
    • The collection phase refers to a specific recycling strategy.

There are two main ways of judging the stage

    • Reference count, object each more than one reference count plus 1, less a reference count minus 1, a count of 0 o'clock indicates that the object can be recycled. But there is a drawback to reference counting, and it is not possible to judge the situation of the loop application, so there is the following way
    • Root search, starting with some root objects (GCRoot) to traverse the search, if an object cannot be searched, it means that the object can be recycled.
      Can be used as an object for gcroot:
      1 一些虚拟机栈中的对象;2 方法区中的类静态属性对象;3 方法区中的常量对象;4 Native栈中JNI的引用对象

There are four main ways of collecting stage

    • Tag cleanup, the simplest algorithm to tell tagged objects to clear directly, fast, but inefficient, memory fragmentation
    • The copy algorithm, which uses half of the available memory each time, copies the available objects to the other half of the memory at collection time, recovering half
    • Tagging, organizing the surviving objects to one end of the memory area, and recovering the remaining parts
    • Generational recovery, the memory area according to the object survival cycle divided into the youth generation and the old age, etc., different regions adopt the above different collection algorithm.
Dalvik and Art

Android5.0 before using the Dalvik virtual machine, and then using the art virtual machine, here are some comparisons:

    • Dalvik converts bytecode to machine code at runtime, art is converted to machine code when it is installed, so installed applications take up more space, but run less time to convert, so it runs faster
    • Art provides better garbage collection performance by reducing the number of pauses in the program from two (analysis, cleanup) to one at a time when the program is paused, garbage collection processing in parallel while recovering newly allocated, short-life objects, and less time spent by the garbage collector
Android Memory Analysis Tool # # Memory MONITORGC operations need to pause other threads, so a short time-frequent GC can have an impact on the UI thread, causing frequent GC to have two common cases, where a large number of objects are created and released immediately in a short time. For example, creating an object in the OnDraw method of the view-young generation's memory area reaches the threshold, and when the remaining space is not enough, it also triggers frequent gcandroid studio provides a memory monitor to show real-time usage of the Application runtime , the blue part below is the memory that is now occupied, and the gray section above shows the recovered memory. If you see spikes on the graph, that is, the rapid allocation of memory is recycled, that is, memory jitter, this is where you need to optimize.

Allocation Tracking

Allocation tracking is a memory tool in DDMS that shows memory allocations over time.

Select the process name you want to track, click Start tracking to begin tracking, do some action and click Get allocations to display the new assigned object in this operation, click on the specific object can see which method is assigned to this object.

Heap Tool and MAT

The Heap tool can view the current memory snapshot

From the data you can see the current memory consumption and recycling, each garbage collection here data will be updated, because it will continue to get memory data refresh display, so this time on the application operation will appear in Dayton.
The Heap tool provides a general picture of the memory, the chart is relatively simple, and if you want to analyze it, it is best to generate a. hprof file, which is analyzed using the mat tool.

There has been a lot of introduction to the use of Mat tools, Google has written a use of the introduction http://android-developers.blogspot.com/2011/03/ Memory-analysis-for-android.html, recommend a Chinese blog,
http://blog.csdn.net/guolin_blog/article/details/42238633, it's very well written.

The Mat tool is commonly used to analyze memory because an application has a memory leak and needs to analyze where it might leak, and then use the Mat tool to verify it. The recent open source of a memory leak detection project by Square Leakcanary greatly simplifies this process, which is the ultimate weapon for Android memory leak detection.

Leakcanary

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

Use:
Add in the Build.gradle file

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

Added in the application OnCreate method of the application LeakCanary.install(this) , as follows

publicclass ExampleApplication extends Application {  @OverridepublicvoidonCreate() {    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.

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/

Reference

Http://sr1.me/way-to-explore/2014/06/20/what-is-art-to-android.html
Http://www.infoq.com/cn/news/2014/07/art-runtime
http://android.jobbole.com/80926/
http://www.liaohuqiu.net/cn/posts/leak-canary-read-me/

Android Memory Analysis Tool

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.