前言:
垃圾回收的几种情况:
GC_FOR_MALLOC means that the GC was triggered because there wasn't enough memory left on the heap to perform an allocation. Might be triggered when new objects are being created.
GC_EXPLICIT means that the garbage collector has been explicitly asked to collect, instead of being triggered by high water marks in the heap. Happens all over the place, but most likely when a thread is being killed or when a binder communication is taken down.
There are a few others as well:
GC_CONCURRENT Triggered when the heap has reached a certain amount of objects to collect.
GC_EXTERNAL_ALLOC means that the the VM is trying to reduce the amount of memory used for collectable objects, to make room for more non-collectable.
中文理解(Copy的)
- GC_MALLOC, 内存分配失败时触发
- GC_CONCURRENT,当分配的对象大小超过384K时触发
- GC_EXPLICIT,对垃圾收集的显式调用(System.gc)
- GC_EXTERNAL_ALLOC,外部内存分配失败时触发
freed 199K表示本次垃圾收集释放了199K的内存,
53% free 3023K/6343K,其中6343K表示当前内存总量,3023K表示可用内存,53%表示可用内存占总内存的比例。
external 0K/0K,表示可用外部内存/外部内存总量
paused
2ms+2ms,第一个时间值表示markrootset的时间,第二个时间值表示第二次mark的时间。如果触发原因不是GC_CONCURRENT,这一行为单个时间值,表示垃圾收集的耗时时间。
调用垃圾回收的原因:
typedefenum{
}GcReason;
垃圾回收调试信息解读(就是LogCat中那些GC信息):
Roughly speaking, the format is [Reason] [Amount Freed], [Heap Statistics], [External Memory Statistics], [Pause Time]
Reason
Robert/yuku already gave info on the meaning of these.
Amount Freed
E.g. freed 2125K
Self explanatory
Heap Statistics
E.g. 47% free 6214K/11719K
These numbers reflect conditions after the GC ran. The "47% free" and 6214K reflect the current heap usage. The 11719K represents the total heap size. From what I can tell, the heap can grow/shrink, so you will not necessarily have an OutOfMemoryError if you hit this limit.
External Memory Statistics
E.g external 7142K/8400K
Note: This might only exist in pre-Honeycomb versions of Android (pre 3.0).
Before Honeycomb, bitmaps are allocated external to your VM (e.g. Bitmap.createBitmap() allocates the bitmap externally and only allocates a few dozen bytes on your local heap). Other examples of external allocations are for java.nio.ByteBuffers.
Pause Time
If it""s a concurrent GC event, there will be two times listed. One is for a pause before the GC, one is for a pause when the GC is mostly done. E.g. paused 3ms+5ms
For non-concurrent GC events, there is only one pause time and it""s typically much bigger. E.g. paused 87ms
解决方法:
第一种:
android不同设备单个进程可用内存是不一样的,可以查看/system/build.prop文件。
# This is a high density device with more memory, so larger vm heaps for it.
dalvik.vm.heapsize=24m
上面heapsize参数表示单个进程可用的最大内存,单如果存在如下参数:
dalvik.vm.heapgrowthlimit=16m
largeheaplimit参数表示单个进程内存被限定在16m,即程序运行过程中实际只能使用16m内存,不过有一个办法可以解决,编辑AndroidManifest.xml中的Application节点,增加属性largeheap="true"参数.
第二种:
第三种:
private final static floatTARGET_HEAP_UTILIZATION = 0.75f;
VMRuntime.getRuntime().setTargetHeapUtilization
这种是修改内存使用率,在系统GC的时候可以尽量提高回收的效率。