Reference article: Heap Viewer
What can the Heap viewer do?
- Real-time view of the app's allocated memory size and free memory size
- Discover the memory Leaks
Heap Viewer conditions of Use
- More than 5.0 of the system, including 5.0
- Developer options available
Heap Viewer Startup
You can directly click on the small robot launcher directly in the Android Studio toolbar:
You can also have tools in the menu bar of Android Studio:
If you don't use Android studio, you can open the monitor program under tools under the SDK:
Heap Viewer Panel
By pressing the tag order, we can see the specific data of the memory, the value in the right panel will change every time the GC, including the app automatically triggered or you to manually trigger.
OK, now to explain the noun in the bottom plate
General overview
Column Name |
meaning |
Heap Size |
The amount of memory allocated to the app by the stack |
Allocated |
Amount of memory allocated for use |
Free |
Free memory size |
%used |
Allocated/heap Size, utilization |
Objects |
Number of objects |
Details
type |
meaning |
Free |
The Idle Object |
Data Object |
Data Objects |
Class object |
Class object |
1-byte Array (byte[],boolean[]) |
An array object of one byte |
2-byte Array (short[],char[]) |
Two-byte array object |
4-byte Array (long[],double[]) |
4-byte Array object |
Non-java Object |
Non-Java objects |
Here is the meaning of the column names for each object:
Column Name |
meaning |
Count |
Number |
Total Size |
Total amount of memory consumed |
Smallest |
Objects occupy memory size from small to large, the first object occupies memory size |
Largest |
The size of the object that takes up memory from small to large rows, and the last object that occupies the memory size |
Median |
The size of the objects in the middle of the memory size from small to large rows |
Average |
Average |
When we click on a row, we can see the following bar chart:
The horizontal axis is the memory size of the object, which is different with different objects, and the ordinate is the number of objects on a certain memory size
Use of the Heap Viewer
We say that the heap viewer is suitable for memory leaks, do you know what a memory leak is?
Memory leaks
English Name: Memory Leaks
Standard explanation: Useless simple, but still no GC root reference memory
Popular Explanation: Damn Undead Memory
Detection
So how to detect it? The values in the Heap Viewer are automatically updated each time the GC occurs, so are we waiting for his own GC? Little brother, just at the beginning I was so always waiting ah, because the timing of the GC is the system grasp, so it is very difficult to grasp, since we are to look at memory leaks, then we need to detect memory leaks in the case after the execution, manual GC, and then observe (Heap size/allocated) memory situation, Look at the memory is not going back to a stable value, after multiple operations, as long as the memory is stable at a certain value, then there is no memory overflow, if found within each GC, is growing, whether it is slow growth or rapid growth, all indicate the possibility of a memory leak.
So what does the Memory Object detail table do? We just need to look at the numbers in the overview table to know what the memory leaks is, and what else does it do? Knowing that the memory leaks will be analyzed in what place, the value of the object details table will be updated after each GC, we can through each update, rough positioning to which type of object has been increased, so that we need to find the scope of the object is much smaller, For example, in the event of a memory leak, we find that only class object
the increase (of course, this is extreme, I just make a metaphor), we use the mat or allocation tracker time will only look at the memory of the class object, shaving the byte[]/char[] such as array objects and other objects.
Add
The heap viewer is not only used to detect memory leaks, but we can also use this tool to detect memory jitter, because when memory jitter occurs, GC is frequent, and we only need to open the heap viewer to observe the changes in the data, and if memory jitter occurs, The data is observed to be updated frequently during the time period.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The heap Viewer tool for Android special tests