Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under What Conditions Does ART Compact the Heap?

Tags:

android

People also ask

How does V8 garbage collection work?

In V8, the garbage collector is named Orinoco. It divides the heap memory space into 2 regions: young generation and old generation. And the young/old generation take different strategies. The minor GC for the young generation applies a much faster but space consuming algorithm called Scavenge.

How does G1 GC work?

G1 GC uses the Snapshot-At-The-Beginning (SATB) algorithm, which takes a snapshot of the set of live objects in the heap at the start of a marking cycle. The set of live objects is composed of the live objects in the snapshot, and the objects allocated since the start of the marking cycle.

Does V8 have garbage collection?

There are two garbage collectors in V8. The Major GC (Mark-Compact) collects garbage from the whole heap. The Minor GC (Scavenger) collects garbage in the young generation.

How does the Java 8 G1 garbage collector work?

G1 uses a pause prediction model to meet a user-defined pause time target and selects the number of regions to collect based on the specified pause time target. The regions identified by G1 as ripe for reclamation are garbage collected using evacuation.


Putting the pieces of the puzzle together.

From what I can determine, ART will compact anything that is paused for 2-3 seconds and by paused it means not currently running in background, so activities, but not running services. It will also compact on the fly, or concurrently while the app is in the foreground.

Currently, the event that triggers heap compaction is ActivityManager process-state changes. When an app goes to background, it notifies ART the process state is no longer jank “perceptible.” This enables ART do things that cause long application thread pauses, such as compaction and monitor deflation.

Chet Hasse states:

Garbage Collection
ART brought improved garbage collection dynamics. For one thing, ART is a moving collector; it is able to compact the heap when a long pause in the application won’t impact user experience (for example, when the app is in the background and is not playing audio). Also, there is a separate heap for large objects like bitmaps, making it faster to find memory for these large objects without wading through the potentially fragmented regular heap. Pauses in ART are regularly in the realm of 2–3ms.

From what I can see any pause in the app is fair game for the ART GC.

I suspect the app needs to be paused completely of all services, etc for the compact to occur, as it's reallocating the memory addresses of the heap, and for this to occur, it cannot be changing. As this larger compact that is taken during the app pause and not on the fly is a dynamic rearrangement of the heap. The only changes that can be made in the smaller pauses is to re-route some addresses on processes no longer being used.

Though this is an educated guess, not definitive and I will endeavour to get more info.

The source code here should have the answer. They're using naming like InJankPerceptibleProcessState() and trying to wade through this, as you probably already have yourself.

Reading it, will update answer when/if I find the definite answer.