[转]Android GC机制及一些调试信息

在Davilk中,给程序分配的内存是根据机型厂商的不同而不同(现在大部分为32MB)

 

在VM内部会将内存分为:java使用的内存,Native使用的内存,他们之间不能共享,当某一方面不足

的时候必须向VM申请,而不能直接使用另外一个的内存。

 

 

android GC

 

if you wanna a concrete example ,please refer to http://www.mysjtu.com/page/M0/S677/677822.html     :D

GC_FOR_MALLOCmeans 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_EXPLICITmeans 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_CONCURRENTTriggered when the heap has reached a certain amount of objects to collect.

GC_EXTERNAL_ALLOCmeans that the the VM is trying to reduce the amount of memory used for collectable objects, to make room for more non-collectable.

I also found this in the Android sources, dalvik/vm/alloc/Heap.h. May this be useful.

typedefenum{
    /* Not enough space for an "ordinary" Object to be allocated. */
    GC_FOR_MALLOC,
    /* Automatic GC triggered by exceeding a heap occupancy threshold. */
    GC_CONCURRENT,
    /* Explicit GC via Runtime.gc(), VMRuntime.gc(), or SIGUSR1. */
    GC_EXPLICIT,
    /* GC to try to reduce heap footprint to allow more non-GC'ed memory. */
    GC_EXTERNAL_ALLOC,
    /* GC to dump heap contents to a file, only used under WITH_HPROF */
    GC_HPROF_DUMP_HEAP
}GcReason;

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

 

出现内存泄漏的可能性:

出现情况:
1. 数据库的cursor没有关闭

2.构造adapter时,没有使用缓存contentview
  衍生listview的优化问题-----减少创建view的对象,充分使用contentview,可以使用一静态类来优化处理getview的过程

3.Bitmap对象不使用时采用recycle()释放内存

4.activity中的对象的生命周期大于activity
调试方法: DDMS==> HEAPSZIE==>dataobject==>[Total Size]

 

1. verbosegc

一般Java虚拟机要求支持verbosegc选项,输出详细的垃圾收集调试信息。dalvik虚拟机很安静的接受verbosegc选项,然后什么都不做。dalvik虚拟机使用自己的一套LOG机制来输出调试信息。

如果在Linux下运行adb logcat命令,可以看到如下的输出:
D/dalvikvm( 745): GC_CONCURRENT 
freed 199K, 53% free 3023K/6343K,external 0K/0K, paused 2ms+2ms 

其中D/dalvikvm表示由dalvikvm输出的调试信息,括号后的数字代表dalvikvm所在进程的pid。

GC_CONCURRENT表示触发垃圾收集的原因,有以下几种:

 

  • 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,这一行为单个时间值,表示垃圾收集的耗时时间。

2. 分析

(1)虽然dalvikvm提供了一些调试信息,但是还缺乏一些关键信息,比如说mark和sweep的时间,

分配内存失败时是因为分配多大的内存失败,还有对于SoftReference,WeakReference和PhantomReference的处理,每次垃圾收集处理了多少个这些引用等。

(2)目前dalvik所有线程共享一个内存堆,这样在分配内存时必须在线程之间互斥,可以考虑为每个内存分配一个线程局部存储堆,一些小的内存分配可以直接从该堆中分配而无须互斥锁。

(3)dalvik虚拟机中引入了concurrentmark,但是对于多核CPU,可以实现parrelmark,即可以使用多个线程同时运行mark阶段。

这些都是目前dalvik虚拟机内存管理可以做出的改进。

原文地址:https://www.cnblogs.com/xiangtailiang/p/3405578.html