Fundamentals 8 min read

Understanding Automatic Garbage Collection and Generational GC in Java

This article explains the concept of automatic garbage collection in Java, detailing the mark‑sweep‑compact process, the need for generational collection, and how objects move through Eden, Survivor, and Old generations, while also noting promotional links for further resources.

Top Architect
Top Architect
Top Architect
Understanding Automatic Garbage Collection and Generational GC in Java

What is automatic garbage collection?

Automatic garbage collection is a mechanism that identifies used and unused objects in heap memory and removes the latter.

Used objects (referenced) have pointers pointing to them; unused objects (unreferenced) have no pointers and can be reclaimed.

In languages like C, programmers manually allocate and free memory, whereas Java relies on a garbage collector to handle memory release.

Step 1: Mark

The garbage collector marks which memory is in use and which is not.

Step 2: Sweep

This step deletes the objects that were marked as unreferenced.

Compaction

After deletion, the remaining referenced objects are compacted to simplify future allocations.

Why is generational garbage collection needed?

Marking and compacting the entire heap is inefficient; most objects die young, so separating the heap into generations improves performance.

JVM Generations

The heap is divided into Young (Eden + Survivor spaces), Old, and Permanent generations.

New objects are allocated in Eden; when Eden fills, a minor GC occurs, moving surviving objects to Survivor spaces and eventually promoting long‑living objects to the Old generation.

Minor GC triggers a "Stop‑the‑World" pause; Major GC (Old generation) also pauses all threads and is slower.

The Permanent generation holds class metadata, which can also be reclaimed when no longer needed.

Generational GC Process

Objects are first allocated to Eden; Survivor spaces start empty.

When Eden fills, a minor GC moves live objects to a Survivor space and clears Eden.

Subsequent minor GCs continue moving objects between Survivor spaces, increasing their age until they are promoted to the Old generation.

Eventually, the Old generation undergoes Major GC, cleaning and compacting the space.

For more details, the article includes links to related resources and promotional messages encouraging readers to follow the WeChat public account for additional guides.

JavaJVMMemory ManagementGarbage CollectionGenerational GC
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.