logcat日志中查看内存GC

logcat日志中查看内存GC

如果程序频繁的出现以下这条日志,那么就需要优化了

D/dalvikvm(3524): GC_EXPLICIT freed 7K, 45% free 3543K/6343K, external 1225K/1615K, paused 68ms

D/dalvikvm:[GC原因] [释放了多少VM内存], [VM堆内存统计], [外部内存统计], [VM暂停时间]

free 3543K/6343K是VM中java使用的内存,external 1225K/1615K是指VM中通过JNI中的Native类中的malloc分配出的内存,例如Bitmap和一些Cursor。
在VM内部会把内存分成java使用的内存和Native使用的内存,它们之间不能共享。

[VM暂停时间]表示会出现操作卡顿,影响体验。

[原因]的内容可以参考Android源码中/dalvik/vm/alloc/Heap.h中enum GcReason的定义:

typedef enum {
/* 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;

 

6san.com

发表评论