Fundamentals 17 min read

Master Java Garbage Collection: Types, Algorithms, and Tuning Tips

This article explains Java garbage collection fundamentals, covering why GC is needed, how it works, reference types, major collection algorithms, generational strategies, specific collectors like CMS and G1, and practical tuning and allocation rules for JVM memory management.

Sanyou's Java Diary
Sanyou's Java Diary
Sanyou's Java Diary
Master Java Garbage Collection: Types, Algorithms, and Tuning Tips

1. What is GC and why?

GC (Garbage Collection) helps release JVM memory, reducing OOM risk but not eliminating it entirely. Java GC runs automatically, unlike manual memory management in C++.

When a new object is created, GC monitors its address and usage, using reachability analysis to find unreachable objects and mark them for possible reclamation.

2. Can you guarantee GC execution?

You cannot guarantee GC; you can request it with System.gc() , but execution is not certain.

3. Types of object references

Strong Reference

Objects referenced strongly are never reclaimed by GC (e.g., objects created with new ).

Soft Reference

Useful but non‑essential objects; they are reclaimed before an OOM occurs, giving a second chance to free memory.

Weak Reference

Even weaker than soft; objects are reclaimed at the next GC regardless of memory pressure.

Phantom Reference

Also called ghost or phantom reference; the object cannot be accessed, and the reference is only used to receive a notification after the object is reclaimed.

4. Garbage collection algorithms

Mark‑Sweep

Two steps: mark reachable objects then sweep away the rest. It has low efficiency and can cause memory fragmentation.

Copying Algorithm

Divides the heap into two equal regions; live objects are copied to the other region when one fills up, then the used region is cleared. Used in the young generation (from/to). It is efficient and avoids fragmentation but halves usable memory.

Mark‑Compact

Marks live objects, then compacts them to one end, clearing the rest. It avoids fragmentation and suits the old generation, though it is slower than copying.

Generational Algorithm

Divides the heap based on object lifetimes (young and old generations). The young generation typically uses the copying algorithm, while the old generation uses mark‑compact.

5. Why use generations?

Scanning the entire heap each time is wasteful; separating objects by lifespan allows using the most appropriate collector for each region, improving efficiency.

6. How does generational GC work?

Objects are allocated in Eden and Survivor spaces. When Eden fills, a Minor GC moves live objects to the “to” Survivor space, clears Eden and the “from” Survivor, then swaps the Survivor spaces. Objects that survive enough cycles (default 15) are promoted to the old generation; large objects are placed directly in the old generation.

When the old generation reaches a threshold, a full GC (usually mark‑compact) is triggered.

7. Garbage collectors

Serial

Single‑threaded, uses copying for the young generation and causes Stop‑The‑World (STW) pauses.

ParNew

Multi‑threaded version of Serial for the young generation, still incurs STW.

Parallel Scavenge

Multi‑threaded copying collector for the young generation, optimized for throughput.

Serial Old

Single‑threaded mark‑compact collector for the old generation, causing STW.

Parallel Old

Multi‑threaded mark‑compact collector for the old generation, throughput‑oriented.

CMS

Concurrent Mark‑Sweep: a concurrent mark‑sweep collector for the old generation that aims for low STW pause times.

G1

Garbage‑First: a global collector using mark‑compact, operating on equal‑sized regions, providing predictable pause times via the -XX:MaxGCPauseMillis parameter.

8. Detailed CMS collector

Four phases: Initial Mark (quick STW), Concurrent Mark (no STW), Remark (shorter STW), Concurrent Sweep (no STW).

Advantages: high concurrency, low pause, short STW.

Disadvantages: CPU‑intensive, cannot collect floating garbage leading to Concurrent Mode Failure, and may cause fragmentation.

9. Detailed G1 collector

Uses mark‑compact, parallel, region‑based collection. Features parallel and concurrent execution, built‑in generational collection, no fragmentation, and predictable pauses.

The heap is divided into many equal regions; G1 tracks the amount of garbage in each region and reclaims the highest‑value regions within a pause budget.

Remembered Sets avoid full‑heap scans.

Phases: Initial Mark (STW), Concurrent Mark, Final Mark (STW), Cleanup (region selection based on pause target).

10. GC log analysis

11. When do Minor GC and Full GC occur?

Minor GC (YGC) occurs when the Eden space is full.

Full GC occurs when the old generation is full, the permanent generation is full, or when System.gc() is invoked.

12. Young and old generation collectors

Young generation: Serial, ParNew, Parallel Scavenge

Old generation: Serial Old, Parallel Old, CMS

Whole‑heap collector: G1

Young generation typically uses copying (high efficiency, low memory utilization). Old generation uses mark‑compact, suitable for large objects and avoiding fragmentation.

13. What is stack allocation?

The JVM can allocate thread‑private objects on the stack instead of the heap. Stack‑allocated objects are reclaimed automatically when the stack frame exits, reducing GC pressure.

Supplement 1: Object escape

Escape analysis determines whether an object may be accessed outside its defining method. Non‑escaping objects can be allocated on the stack.

<code>private User user;
private void hello() {
    user = new User();
}
</code>

Here user is a field, thus it escapes.

<code>private void hello() {
    User user = new User();
}
</code>

Here user does not escape and may be stack‑allocated.

Supplement 2: TLAB

Thread‑Local Allocation Buffer allows objects that would normally be on the heap to be allocated in a thread‑private area, reducing GC pressure.

14. Object allocation rules

Objects are first allocated in Eden; if insufficient, a YGC moves survivors; if still insufficient, allocation guarantee may place them in the old generation.

Large objects go directly to the old generation to avoid copying.

Objects that survive multiple YGCs are promoted based on age (default threshold 15, configurable via -XX:MaxTenuringThreshold ).

Dynamic age promotion may move objects to the old generation earlier if Survivor space is crowded.

Allocation guarantee may trigger a Full GC if promoting objects would exceed the remaining old‑generation space.

JavaJVMmemory managementGarbage CollectionGC AlgorithmsCMSG1
Sanyou's Java Diary
Written by

Sanyou's Java Diary

Passionate about technology, though not great at solving problems; eager to share, never tire of learning!

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.