Fundamentals 9 min read

Understanding G1 Garbage Collector: Concepts, Heap Regions, Pause Prediction, Object Header, and Allocation Strategies

This article explains the core concepts of the G1 garbage collector, including its memory management duties, heap region types and sizing, pause‑time prediction model, card table and bitmap usage, object header structure, and both fast and slow object allocation strategies.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Understanding G1 Garbage Collector: Concepts, Heap Regions, Pause Prediction, Object Header, and Allocation Strategies

Basic Concept : In JVM implementations, GC accurately refers to the memory manager, which handles both memory allocation management and garbage collection; the two are inseparable.

Heap Regions (Heap Region, HR) : G1 divides the heap into equal‑sized regions, classified as Free (FHR), Young (YHR), Humongous (HHR), and Old (OHR). Young regions further split into Eden and Survivor; humongous regions split into head and contiguous parts. Region size can be set via the G1HeapRegionSize parameter or inferred heuristically, with allowed sizes of 1 MB, 2 MB, 4 MB, 8 MB, 16 MB, and 32 MB, defaulting to 2048 regions.

Pause Prediction Model : G1 is a response‑time‑first GC algorithm; users can specify an expected pause time with MaxGCPauseMillis (default 200 ms), but the JVM only strives to meet this target.

Card Table and Bitmap : G1 retains the CardTable concept from CMS and introduces RSet. Card tables mark memory reference relationships to quickly traverse live objects, while bitmaps describe object allocation during concurrent marking.

Object Header : Java objects have a header containing mark information (stored in MarkOop) and metadata (a reference to the Klass object). The header holds lock, GC, and hash information and is only needed when biased locking is used, the object is locked, or a hash code is set.

Object Allocation Overview : Allocation efficiency impacts both memory usage and GC performance. G1 provides two strategies: fast allocation via Thread‑Local Allocation Buffers (TLAB) and slower allocation that may trigger GC.

Fast Allocation (TLAB) : Each thread gets a private buffer to avoid global heap locks. Allocation proceeds in two steps: (1) try to allocate from the thread’s TLAB; if successful, return the object, (2) if not, allocate a new TLAB and then the object. TLAB allocation itself uses CAS for concurrency.

Slow Allocation : When TLAB allocation fails, the JVM attempts a slower path that may lock the heap, expand the young generation, or invoke a GC (often a Full GC). The process includes attempts like attempt_allocation , handling humongous objects, and possibly multiple GC cycles before giving up.

Large Object Allocation : Similar to slow allocation but handles objects larger than half a region size. The JVM may allocate whole free regions or multiple contiguous regions, using locks as needed, and may perform incremental or concurrent marking before allocation.

Final Allocation Attempts : The JVM makes final tries by expanding new regions, performing Full GC without collecting soft references, and finally performing Full GC with soft‑reference collection; if all attempts fail, allocation returns NULL.

JavaGarbage CollectionG1 GCHeap Regionsobject allocation
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.