轉自:http://www.poemcode.net/2012/06/interpreting_log_message/
本篇博文是在觀看 Google
I/O 2011 : Memory Management for Android App 所做的筆記之一,主要篇幅是傳遞演講者 Patrick
Dubroy 的觀點,小部分則摻雜了自己的理解。這裡的觀點可能包含了謬誤,或者是過時資訊,因此建議讀者如果英語能力尚可,則不妨直接觀看原視頻或者簡報,–需翻牆。
Memory Management for Android Apps
Android 日誌是開發人員審視系統、應用健全狀態的得力助手,它就像是駕駛員面前的儀錶盤,車輛的時速、裡程、油量等資料關係到行駛安全,而日誌中的 GC 資訊則反饋了系統、應用健康情況,細心的開發人員甚至可以判定是不是發生了記憶體流失。
以下是我截取到的三則 GC 資訊,它們分別代表了三種常見情境。我曾相同代碼在不同版本,比如2.2(Froyo), 2.3(Gingerbread)和4.0.2(Ice cream sandwich),發現 GC 資訊變化比較大,這也印證了演講者關於 Dalvik VM GC 在 2.3 和 3.0 發生了改變的說法。
GC_CONCURRENT freed 2234K, 35% free 9013K/13767K, external 821K/1435K, paused 9ms+9ms
GC_FOR_MALLOC freed 689K, 33% free 8959K/13319K, external 1451K/1688K, paused 82ms
GC_EXTERNAL_ALLOC freed 122K, 38% free 8460K/13511K, external 1519K/1519K, paused 78ms
從上面可以看出GC 資訊可以分成幾個部分,例如 Reason for GC、Amount freed 等,以下將它們拆分來看。
Reason for GC
演講者共提到五種類型,其中 GC_EXPLICIT 不為提倡,比較少見,GC_HPROF_DUMP_HEAP 則在正常運行情況下不會發生。GC_CONCURRENT 則從 2.3 版本才出現。同時 GC_FOR_MALLOC 則去掉了“M”。
-
1. GC_CONCURRENT
-
Triggered by basically, as your heap starts to fill up. VM kick off the current garbage collection so that it can hopefully complete before the heap gets full.
-
2. GC_FOR_MALLOC / GC_FOR_ALLOC
-
VM don’t complete the concurrent collection in time and application has to allocate more memory. The heap was full, so VM had to stop and do a garbage collection.
-
3. GC_EXTERNAL_ALLOC
-
Externally allocated memory, like bitmap pixel data. It’s also used for NIO direct byte buffers. The external memory has gone away in honeycomb, and basically everything is allocated inside the Dalvik heap now.
Now this external memory has gone away in Honeycomb. Basically everything is allocated inside the Dalvik heap now. So you won’t see this in your log messages in Honeycomb and later.
-
4. GC_HPROF_DUMP_HEAP
-
When you do a hprof profile.
-
5. GC_EXPLICIT
-
When call system.gc, GC_EXPLICIT happen. Programmer should avoid doing this, and trust in the garbage collector.
Amount freed
The amount of memory that was freed on this collection
Heap statistics
xx% freed after the collection completed.
The 1st number is live objects
The 2nd number is the total heap size
External memory statistics
which is bitmap pixel data and also NIO direct byte buffers.
The 1st of number is the amount of external memory that your app has allocated
The 2nd number is a sort of soft limit. When you’ve allocated that much memory, we’re going to kick off GC.
這裡,我有一個疑問就是,演講者提到 2.3 以後 external memory 的資料(bitmap pixel data)被移動到了 Dalvik Heap,那麼到底是什麼資料儲存到了這塊法外之地?
Pause time
One short pause at the beginning of the collection and one most of the way through. Non-concurrent collections you will see a single pause time.
這裡,我有一個疑問就是,非並發類 GC 只有一個耗時好理解,但為何並發類 GC 卻有兩個值,從字面上理解演講者的話,就是有兩個耗時,分別發生在開始時和結束時,那麼中間值跑去哪裡了?