1. View the log information in the IDE
When the program performs a garbage collection, a log message is printed. Its format is as follows:
D/DALVIKVM: <GC_Reason> <amount_freed>,
Gc_reason represents the cause of garbage collection and the current collection type, including the following categories:
Gc_concurrent: Garbage collection that is triggered when the number of objects in the heap reaches a certain level
Gc_for_malloc: Allocates memory when memory is full and the system pauses the program and reclaims memory
Gc_hprof_dump_heap: Creating a fpfor file to analyze garbage collection caused by HEAP
GC_EXPLICIT: The program called the garbage collection function System.GC
Gc_external_alloc: Out of today API 10 and below. Garbage collection caused by external allocation of memory (native memory or NIO buffer), the high version number is allocated in the Dalvik heap.
Amount_freed indicates that the recovered memory
Heap_stats represents spare memory percent and the total size of the surviving object/heap
External_memory_stats represents API 10 and the external memory allocated below. Allocated memory/boundaries that cause garbage collection
Pause_time pause time. An indication of when the garbage was started. There is also a pause time that indicates the end of the recycle
D/DALVIKVM (9050): Gc_concurrent freed 2049K, 65% free 3571k/9991k, external 4703k/5261k, paused 2ms+2ms
Note the "3571k/9991k" value in this message, which represents the heap size used by the program.
2. Using DDMS
Ddms in Eclipse provides a GUI that observes memory usage when we repeatedly click Cause GC. You will see the current program heap, the use of more convenient, detailed use of the method can be Google a bit.
3. Use the ADB dumpsys command
ADB is a powerful tool for viewing application memory usage using ADB to view memory usage on the command line in the following format:
adb shell Dumpsys meminfo <package_name>
Among them, package_name can also be replaced by the PID of the program. PID can be passed through adb shell top | grep app_name to find, is the memory usage of a program:
Focus on such fields as the following:
(1) Heap information for Native/dalvik
Detailed in the first and second lines above, it gives the JNI layer and the Java layer of memory allocation, assuming that the value has been increasing, it represents a program may have a memory leak.
(2) PSS information for total
This value is the amount of memory your app really occupies, through this information. You can easily determine which programs in your phone account for a larger memory.
4. Using adb shell Procrank
The SH in the cell phone is streamlined. Some phones may not have procrank commands. Ability to use the Genymotion simulator. or install the Procrank command yourself. When using Procrank, the output of the command line is entered:
You can see that there are four different representations of memory consumption under Linux:
Vss-virtual Set Size Virtual memory consumption (including memory consumed by shared libraries)
Rss-resident Set Size actually uses physical memory (including memory consumed by shared libraries)
Pss-proportional Set Size Actual physical memory used (proportional allocation of memory consumed by shared libraries)
Uss-unique the Set Size process consumes the physical memory alone (excluding memory consumed by shared libraries)
Vss:vss represents the size of all memory address spaces that a process can access.
This size contains the memory space that the process has requested but not yet used. In practice, it is very rare to indicate that a process consumes memory, and it is not accurate to use it to represent the memory usage of a single process.
RSS: Represents the space address size that a process actually uses in RAM. Contains all the memory occupied by the shared library. It is also inaccurate to indicate that the process is consuming memory.
PSS: Represents the space address size that a process actually uses in RAM, which proportionally includes the memory consumed by the shared library. If 3 processes use the same shared library, the PSS for each process includes a 1/3-size shared library memory.
This means 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: Represents the amount of memory space that a process itself occupies, not including any other components, which is the best way to represent the size of the process memory!
can see: Vss>=rss>=pss>=uss
5. Other frequently used command commands:
adb shell kill Pidnumber The background process you want to kill to simulate the recurrence condition of a bug.
adb shell PS View process information in the current terminal
So how do you infer how much RAM the current hardware system has in your code? For example, the following code is available in the Framework Processlist.java:
Processlist () {
Meminforeader minfo = new Meminforeader ();
Minfo.readmeminfo ();
MTOTALMEMMB = Minfo.gettotalsize ()/(1024*1024);
}
To view the CPU usage of a process: adb shell top-n 1-d 0.5 | grep proc_ ID
View memory usage set in Android frequently using ADB commands