您的位置:首页 > 移动开发 > Android开发

Android ART 垃圾回收 机制&算法

2017-08-31 14:48 666 查看
之前有被问到过一个面试题,问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技术。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android ART 垃圾回收