Analysis of memory problems in Android Development (I) Tools

Source: Internet
Author: User

Analysis of memory problems in Android Development (I) Tools

Memory leakage and memory overflow are common problems in Android development. How to quickly and effectively discover and track the source of Memory leakage or memory overflow is a skill that every developer must master, today, I will introduce you to common memory analysis tools and hope to help you in future development.

Use Eclipse to analyze application memory usage

The procedure is as follows:
1. After eclipse is started, switch to the DDMS perspective and open the Devices view and Heap view through Window-ShowView.

2. After connecting to a mobile phone or simulator, the serial number of the mobile phone device or simulator is displayed in the Devices view of DDMS, and information about some processes running on the device.

3. Click to select the process to be monitored, and then click Update Heap to automatically find the process of the running application.


4. click the "Cause GC" button in the Heap view to request a GC operation (PS: When the memory usage information is displayed for the first time, you do not need to constantly click "Cause GC ", the Heap View Interface is refreshed on a regular basis. You can see the changes in memory usage during the ongoing operations on the application)

5. Click Cause GC to view the memory usage of our application, as shown in the following figure:


6. During the specific use process, we should pay attention to a Type in the Heap view called data object, that is, the data object, that is, a large number of existing class objects in our program. In a row of data object, there is a column named "Total Size", whose value is the Total memory of all Java data Objects in the current process, if you want to see that "Total Size" is the specific allocation information, click "data object" to view the details, such:

In use, the value "Total Size" determines whether memory leakage exists. We can continuously operate the current application and observe the changes in the Total Size of data Objects. Under normal circumstances, the Total Size value will be stable within a limited range, that is to say, because the code in the program is good, there is no case that the object will not be garbage collected, so although we will continuously generate many objects, during the continuous GC of virtual machines, these objects are recycled, and the memory usage will reach a stable level. Otherwise, if the Code does not release the object reference, the Total Size value of the data object does not significantly decrease after each GC. As the number of operations increases, the Total Size value increases. Normally, the process of a virtual machine exists 64 mb. If the memory leaks, Heap Size is constantly approaching 64 mb. Once this value is reached, the memory leaks and exits from the application.
When the value of Total Size is getting bigger and bigger, we press the "Dump HPROF File" button and a prompt is displayed to set the path for saving the hprof File. After saving, we can use the MAT tool to analyze which operations cause memory leakage.

Use MAT to analyze the hprof File

Eclipse Memory Analyzer (MAT) is one of 33 components of the famous cross-platform integrated development environment Eclipse Galileo version. It is a function-rich JAVA heap dump file analysis tool, this helps you discover memory vulnerabilities and reduce memory consumption. For large JAVA applications, it is difficult to block all the vulnerabilities even if we perform a lot of effective work in the test phase, many problems will still be exposed in the production environment, and it is difficult to reproduce in the testing environment. The JVM can record part of the running status of the system when a problem occurs and store it in the Heap Dump file, which provides an important basis for us to analyze and diagnose the problem.
Download the latest MAT tool from the official website:

The steps for using MAT are as follows:
1. Open the MAT tool, File-> Open Heap Dump... Select the hprof file you just saved to open
An error is displayed, as shown in:

Do not assume that the MAT tool version is incorrect. In fact, the hprof file of android needs to be converted here to use MAT to open it. Use AndrodiSDK/tools/hprof-conv to convert the hprof file,
First, go to your android sdk tools directory through the console
For example, hprof-conv input. hprof out. hprof
Use the MAT tool to open the converted hprof file and you will be able to see the complete memory usage analysis report.

2. As shown in the following figure, the main interface used by MAT for memory analysis:

Click Reports-> Leak Suspects to see more details about memory leakage.

Click Details to view the memory usage.

We can see that the memory aggregation point is a collection with a large number of objects. This object collection stores a large number of Person object references, that is, it causes memory leakage.
3. Histogram queries are often used.

Right-click an item and choose list objects-> with incoming references from the menu to list the instances of this class. You can also click the header to sort, in the first row of the table, you can enter a regular expression to match the result.

4. Right-click a selected item and choose list objects> with incoming refs from the shortcut menu to list instances of this class:

The new Tab page displays the reference relationships between objects.

To view the reason why an instance is not released, right-click the Path to GC Roots-> exclue all phantom/weak/soft etc. reference: in this case, you can quickly view the GC Root of an object.

5. Sometimes we need to Compare two Dump results to find memory leaks. In this case, we can open the Navigator History panel and add the Histogram results of the two tables to Compare Basket:

Click the red exclamation mark in the upper-right corner of the Panel to get the comparison result:

You can also change the comparison conditions:

Use AndroidStudio to analyze application memory usage

Currently, developers generally use AndroidStudio for development. The analysis on memory usage is more user-friendly, and it also supports analysis of hprof files by default. Let's take a look at the following:
(1) Use AndroidStudio to track Memory usage, open the Android Monitor Panel (PS: shortcut: Alt + 6), and click the Memory Tab

(2) Use AndroidStudio to analyze the hprof file. The specific steps are as follows:
1. Open Android Device Monitor in the menu bar of AndroidStudio, for example:

2. on the Android Device Monitor page, select the package name of the application you want to analyze, click Update Heap to Update statistics, and then click Cause GC to view the usage of the current Heap, as shown in figure

3. Click Dump HPROF file to save the current memory information of the application as an hprof file and store it in the specified path, as shown in figure

4. Open the saved hprof File in the File menu bar (you can also drag the hprof File into AndroidStudio to save trouble)

5. In the opened hprof file, we can easily find the package name of our application and open the corresponding package to see the memory usage.

Use LeakCanary to analyze application memory usage

LeakCanary is an open-source java library that detects memory leaks in the debug version.
GitHub address: https://github.com/square/leakcanary
(1) configuration instructions:

(2) Use Cases:

Create a RefWatcher instance and give it an object for its observation:
RefWatcher. watch (schrodingerCat );
When a leak is detected, you will automatically get a beautiful leak clue:

Gc root static Docker. container references Box. hiddenCat leaks Cat instance

Then, with just a few lines of code, LeakCanary can automatically detect Activity leaks: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxpbWcgYWx0PQ = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/160414/042455IO-30.png" title = "\"/>

When the memory is insufficient, you will see the following notification page:

That is to say, enabling LeakCanary can help us more easily identify and fix many memory problems.

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.