Android ART 垃圾回收 機制&算法

之前有被問到過一個面試題,問Android裏的垃圾回收算法是什麼?當時有點懵逼,後來查了一下原來Android官網是有說明的,原文地址鏈接如下:
https://source.android.com/devices/tech/dalvik/gc-debug

以下是摘抄,自行翻譯:
ART has a few different GC plans that consist of running different garbage collectors. The default plan is the CMS (concurrent mark sweep) plan, which uses mostly sticky CMS and partial CMS. Sticky CMS is ART’s non-moving generational garbage collector. It scans only the portion of the heap that was modified since the last GC and can reclaim only the objects allocated since the last GC. In addition to the CMS plan, ART performs heap compaction when an app changes process state to a jank-imperceptible process state (e.g. background or cached).

也就是說,可以認爲是CMS(Concurrent Mark-Sweep),但是不同情況又有不同的使用,主要是粘性CMS和分部CMS的區別。

與Dalvik VM的區別:
Compared to Dalvik, the ART CMS garbage collection plan has a number of improvements:

  • The number of pauses is reduced from two to one compared to Dalvik. Dalvik’s first pause, which did mostly root marking, is done concurrently in ART by getting the threads to mark their own roots, then resume running right away.
  • Similarly to Dalvik, the ART GC also has a pause before the sweeping process. The key difference in this area is that some of the Dalvik phases during this pause are done concurrently in ART. These phases include java.lang.ref.Reference processing, system weak sweeping (e.g. jni weak globals, etc.), re-marking non-thread roots, and card pre-cleaning. The phases that are still done paused in ART are scanning the dirty cards and re-marking the thread roots, which helps reduce the pause time.
  • The last area where the ART GC improves over Dalvik is with increased GC throughput enabled by the sticky CMS collector. Unlike normal generational GC, sticky CMS is non-moving. Instead of having a dedicated region for young objects, young objects are kept in an allocation stack, which is basically an array of java.lang.Object. This avoids moving objects required to maintain low pauses but has the disadvantage of having longer collections for heaps with complex object graphs.

大意是:
大概意思是ART通過異步的形式省去了DVM的第一次暫停;
又通過異步的方式較少了清理操作時的時間;
使用sticky-CMS技術。

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