1. view Log information in IDE
When the program is running garbage collection, a Log is printed in the following format:
D/dalvikvm: , , ,
GC_Reason indicates the cause of garbage collection and the current collection type, including the following:
GC_CONCURRENT: It is triggered when the number of objects in the heap reaches
GC_FOR_MALLOC: allocates memory when the memory is full. The system suspends the program and recycles the memory.
GC_HPROF_DUMP_HEAP: Create a FPFOR file to analyze the garbage collection caused by Heap.
GC_EXPLICIT: The program calls the garbage collection function System. gc.
GC_EXTERNAL_ALLOC: Garbage collection caused by the external allocation of memory (native memory or NIO buffer) in API 10 or lower. All the later versions are allocated in Dalvik Heap.
Amount_freed indicates the memory to be recycled this time.
Heap_stats indicates the percentage of idle memory and the size of the surviving object/The total size of the heap.
External_memory_stats indicates the external memory allocated for API 10 and below, and the allocated memory/the boundary that causes garbage collection
Pause_time: The pause time. One indicates the time when garbage collection starts, and the other indicates the pause time when garbage collection ends.
D/dalvikvm (9050): GC_CONCURRENT freed 2049 K, 65% free 3571 K/9991 K, external 4703 K/5261 K, paused 2 ms + 2 ms
Note that the "3571 K/9991K" value in this message represents the heap size used by the program.
2. Use DDMS
DDMS in Eclipse provides a GUI to observe memory usage. When we click Cause GC, we will see the Heap of the current program, which is more convenient to use. You can Google it for specific usage.
3. Use the adb dumpsys command
Adb is a very powerful tool. You can use adb to view the application memory usage in the command line in the following format:
Adb shell dumpsys meminfo
Package_name can also be changed to the pid of the program. The pid can be searched through adb shell top | grep app_name, which is the memory usage of a program:
Focus on the following fields:
(1) Native/Dalvik Heap Information
Specifically, in the first and second rows above, it gives the memory allocation at the JNI and Java layers. If you find that this value keeps increasing, memory leakage may occur in the program.
(2) Total PSS Information
This value is the actual memory occupied by your application. With this information, you can easily identify which programs on your mobile phone occupy a large amount of memory. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + CjxwPjxzdHJvbmc + large + tbQtcRzaMrHvq25/b6rvPK5/large + r/large + large = "http://www.2cto.com/uploadfile/Collfiles/20140629/2014062910055981.jpg" alt = "\">
We can see that in linux, there are four different manifestations of memory consumption:
VSS-Virtual Set Size Virtual memory consumption (including memory occupied by shared libraries)
RSS-Resident Set Size actually uses physical memory (including memory occupied by shared libraries)
PSS-Proportional Set Size physical memory actually used (Proportional allocation of memory occupied by the Shared Library)
USS-Unique Set Size physical memory occupied by the process (excluding the memory occupied by the Shared Library)
VSS: VSS indicates the size of all memory address space accessible to a process. This size includes the memory space applied by the process but not used yet. In practice, this method is rarely used to indicate the memory usage of a process. It is inaccurate to use it to indicate the memory usage of a single process.
RSS: the size of the space address actually used by a process in RAM, including the memory occupied by all the shared libraries. This indicates that the memory occupied by the process is inaccurate.
PSS: the size of the space address actually used by a process in RAM. It contains the memory occupied by the shared library in proportion. For example, if three processes use the same shared database, the PSS of each process includes a 1/3-size shared library memory. This method indicates that the memory usage of the process is more accurate, but when only one process uses a shared library, the situation is exactly the same as RSS.
USS: the memory space occupied by a process. It does not contain any other components. This is the best way to indicate the memory size of the process!
We can see that VSS> = RSS> = PSS> = USS
5. Other Common commands:
Adb shell kill PIDNumberThe background process you want to kill to simulate the recurrence condition of a certain bug.
Adb shell psView process information on the current terminal
So how can we determine the number of RAM resources in the current hardware system in the code? The following code is available in Framework ProcessList. java:
ProcessList (){
MemInfoReader minfo = new MemInfoReader ();
Minfo. readMemInfo ();
MTotalMemMb = minfo. getTotalSize ()/(1024*1024 );
}
View cpu usage of a process: adb shell top-n 1-d 0.5 "grep proc _ id