Android內存使用——垃圾回收LOG,GC_CONCURRENT等的意義的說明

 在調試程序的時候,經常發現GC_CONCURRENT之類的打印。在網上搜了一下,感覺說法各式各樣。最後,在Google的官方網站上發現了詳細介紹。

Every time a garbage collection occurs, logcat prints a message with the following information:

D/dalvikvm: <GC_Reason> <Amount_freed>, <Heap_stats>, <External_memory_stats>, <Pause_time>
GC Reason
What triggered the garbage collection and what kind of collection it is. Reasons that may appear include:
GC_CONCURRENT
A concurrent garbage collection that frees up memory as your heap begins to fill up.
GC_FOR_MALLOC
A garbage collection caused because your app attempted to allocate memory when your heap was already full, so the system had to stop your app and reclaim memory.
GC_HPROF_DUMP_HEAP
A garbage collection that occurs when you create an HPROF file to analyze your heap.
GC_EXPLICIT
An explicit garbage collection, such as when you call gc() (which you should avoid calling and instead trust the garbage collector to run when needed).
GC_EXTERNAL_ALLOC
This happens only on API level 10 and lower (newer versions allocate everything in the Dalvik heap). A garbage collection for externally allocated memory (such as the pixel data stored in native memory or NIO byte buffers).
Amount freed
The amount of memory reclaimed from this garbage collection.
Heap stats
Percentage free and (number of live objects)/(total heap size).
External memory stats
Externally allocated memory on API level 10 and lower (amount of allocated memory) / (limit at which collection will occur).
Pause time
Larger heaps will have larger pause times. Concurrent pause times show two pauses: one at the beginning of the collection and another near the end.

For example:

D/dalvikvm( 9050): GC_CONCURRENT freed 2049K, 65% free 3571K/9991K, external 4703K/5261K, paused 2ms+2ms


GC Reason:引起GC回收的原因。包括a)GC_CONCURRENT:我的理解是當你的堆快被用完的時候,就會觸發這個GC回收。b)堆已經滿了,同時又要試圖分配新的內存,所以系統要回收內存。c)GC_HPROF_DUMP_HEAP這是做HPROF分析用的,正常情況下不會有的。d)這個是明確調用gc產出的垃圾回收。e)GC_EXTERNAL_ALLOC這個在2.3版本以後已經廢棄了。所以不用關心了。
Amount freed:本次垃圾回收釋放出來的內存。
Heap stats:當前空閒內存佔總內存的百分比。比如下面的例子總的65%就是說有65%的空閒。二後面的3571K是佔用了這麼多,9991K是總的內存。這兩個值相除,就是佔用的百分比,加上前面的65%正好是100%。
External memory stats:2.3之後就廢棄了。
Pause time:你的應用暫停的時間。
假如3571K/9991K 這個值不斷增加,從來不減少,則可能有內存泄漏的問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章