[Android] development optimization-tuning tool: dump hprof file to view memory information, find Memory leakage, androidhprof
Although I know the general issues that need to be paid attention to when developing android applications, there are also uneven levels, especially the maintenance code. If the memory usage is large and the memory overflow is serious, how can I solve it? -- Capture and analyze heap through DDMS
1. Open DDMS
2. Select the program to be viewed and click the Update Heap icon.
3. View Heap status
However, this is only to check the Heap usage. If the memory usage or overflow is found to be serious, it will become OOM. We can't see anything through this. In this case, we need to capture and analyze the Heap, and there is a button on the edge of UpdateHeap.
Click it to generate a file and save it as needed (it will be a little delayed, please wait patiently, I will save it to the desktop)
7. view the downloaded file, but we need MAT before you can view it.
8. install the MAT. The steps are basically online. Select install on the Right of Memory Analysis in eclipse-help-Eclipse Marketplace-and then click Next to install Memory Analysis.
9. If the. hprof file is converted, you cannot use MAT to view the files directly saved by DDMS. An incorrect format is displayed. In this case, use the sdk tool hprof-conv for conversion.
C:\Users\Administrator>hprof-conv C:\Users\Administrator\Desktop\test.hprof C:\Users\Administrator\Desktop\hprofed-test.phrof
10. Use MAT to open the. hprof file and check the memory usage.
As for how MAT uses Baidu or Google, there are still a lot of things, or when I study something valuable to android, I will open another blog to explain it!
I used android MAT to check memory leakage. I checked a lot of information. Why is there no hprof file in DDMS?
How to Use MAT for analysis is provided that the Android development and testing tools are fully installed. SDK and Eclipse:
1. Open Eclipse
2. Select Help-> Install New Software;
3. Add the site download.eclipse.org/mat/1.0/update-site/ to Work with (this address may change, but the new address can be found at www.eclipse.org/mat/downloads.php on the official website)
4. generate. hprof file: insert the SD card (many Android programs need to insert the SD card), connect the device to the PC, and select the process to be tested in DDMS of Eclipse, click the Update Heap and Dump HPROF file buttons.
The. hprof file is automatically saved on the SD card, and the. hprof file is copied to the \ android-sdk-windows \ tools directory on the PC. The file generated by DDMS cannot be opened directly in MAT and needs to be converted.
Run cmd to open the command line, cd to the directory where \ android-sdk-windows \ tools is located, and enter the command hprof-conv xxxxx. hprof yyyyy. hprof, where xxxxx. hprof is the original file, yyyyy. hprof is the converted file. The converted files are automatically stored in the android-sdk-windows \ tools directory.
OK. Till now, the. hprof file has been processed and can be used to analyze Memory leakage.
5. Open MAT:
In Eclipse, choose Windows> Open Perspective> Other> Memory Analysis.
6. Import the. hprof File
In the MAT, click File-> Open File to browse to the converted File. hprof file, and Cancel off to automatically generate the report. Click Dominator Tree, group by Package, and right-click the Package class you have defined, select List objects-> With incoming references in the pop-up menu.
All the suspicious classes are listed. Right-click an item and choose Path to GC Roots-> exclude weak/soft references, all classes related to the program with Memory leakage will be further filtered out. Based on this, we can track a class in the code that produces leakage.
How to find Memory leakage in Android
1. First, determine whether there is memory leakage and which program is causing it.
1.1 memory leakage The out of memory dialog box is displayed.
This situation is very simple. You can check the dialog box to find out which application the problem is. Then, analyze whether the application is caused by memory leakage.
Out of memory dialog box.
To determine whether there is a memory leak or which process causes the memory leak.
2. Generate the hprof file and use MAT for analysis.
Generate the hprof file. You can click the dump hprof file button in the upper-left corner of the window in the selected process of DDMS to generate the file directly. You can also add code in the program to generate code 2: voidgenerateHprof () {String packageName = getApplicationInfo (). packageName;
StringhpFilePath =/data/+ packageName +/input. hprof; try {// Debug. dumpHprofData (/sdcard/input. hprof); Debug.
DumpHprofData
(HpFilePath);} catch (IOException e) {// TODOAuto-generated catch block
E. printStackTrace () ;}} we recommend that you use code to generate hprof, and then use 《
Tools in Android Memory leakage tool (hprof) automatically extract multiple hprof files and use MAT for comparative analysis. After MAT imports the. hprof file,
The MAT will automatically parse and generate the report. Click
Dominator Tree
Group by Package, select the Package class you have defined, and compare the RetainedHeap of each class in different periods
, Locate the suspicious class, select the class, right-click, select
Show retained Set item, see Retained Heap
To further identify suspected items.
3. Search for Memory leakage in the code.
Based on the memory leakage information found at the MAT, refer 《
Android Memory leakage
To further find out the cause of Memory leakage in the memory and solve the problem.
If the code is simple, you can directly refer 《
Android Memory leakage
Find and solve the cause of Memory leakage in the memory.