Understanding Android ART CC Collector: GC Challenges, Implementation Principles and Performance Optimization
The article thoroughly explains Android ART’s Concurrent Copying (CC) garbage collector—its terminology, correctness and progress requirements, engineering challenges, classic and Android‑specific algorithms, region‑based design with bump‑pointer allocation, performance benefits such as 32% lower heap usage and sub‑millisecond pauses, and its trade‑offs including doubled reserved heap space and migration overhead.
This article provides an in-depth exploration of JVM garbage collection and the Concurrent Copying (CC) collector implementation in Android ART (Android Runtime).
The content begins by defining key GC terminology including Concurrent (parallel execution of GC and application threads), Parallel (multiple GC threads working simultaneously), Collector (GC execution thread), Mutator (application thread that modifies heap), RootSet (global class and thread context objects), RememberSet (data structure recording cross-generational and cross-region references), CardTable (RememberSet implementation), Tracing (reachability analysis from root objects), and PauseTime (time when Mutator threads are suspended).
The article discusses the three fundamental requirements for a qualified garbage collector: Correctness (never losing live objects), Progress (eventually clearing all garbage), and Termination (ensuring Tracing phase can complete). The main engineering challenges include minimizing PauseTime through concurrent/parallel collection, maximizing throughput, reducing memory allocation overhead, minimizing memory fragmentation, improving memory utilization, reducing GC metadata overhead, and handling floating garbage.
Two primary垃圾对象判定 algorithms are examined: Reference Counting (RC) and Reachability Analysis. RC tracks object reference counts and reclaims when count reaches zero, offering real-time reclamation and high memory utilization but suffering from circular reference issues and overhead. Reachability Analysis identifies live objects from root set and marks all others as garbage, avoiding circular reference problems but requiring stop-the-world pauses.
The article details classic GC algorithms: Generational Collection (based on weak/strong generational hypotheses and cross-generational reference hypothesis), Mark-Sweep (simple but creates fragmentation), Mark-Copy (eliminates fragmentation but wastes half memory), and Mark-Compact (no fragmentation but expensive object movement).
For Android-specific implementation, the article explains that Android 7(N) and earlier used CMS in foreground and HSC (Homogeneous Space Compaction) in background, while Android 8(O) and later adopted CC collector for both foreground and background, with Android 10(Q) reintroducing generational collection.
The CC collector divides the heap into equally-sized Regions (256KB), using BumpPointer for efficient allocation. Its global algorithm is Mark-Compact while between regions it uses Mark-Copy. The collection process includes: InitializePhase, MarkingPhase, FlipThreadRoots, CopyingPhase, and ReclaimPhase. Key mechanisms include write barriers, read barriers, and forwarding pointers to support concurrent operation.
CC collector advantages include: highest memory allocation speed through bump-pointer allocation, memory compaction in both foreground and background, more flexible memory management reducing heap usage by 32% (Google data), and precise generational GC strategies improving throughput and reducing PauseTime (average 0.4ms, independent of heap size).
Disadvantages include: requiring twice the heap space for address reservation, longer concurrent copying time with increasing heap size, object migration storms involving Mutator threads, potential thread blocking from read-write mutex locks, and inability to individually free object memory resulting in floating garbage.
OPPO Kernel Craftsman
Sharing Linux kernel-related cutting-edge technology, technical articles, technical news, and curated tutorials
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.