JVM.What is major, minor, full gc ?

Major GC vs. Minor GC in Java Garbage Collection

Garbage collection (GC) in Java is categorized into Minor GC and Major GC, based on which part of the heap is being cleaned.


1️⃣ Minor GC (Young Generation Collection)

What is Minor GC?

  • A Minor GC is a garbage collection event that only cleans the Young Generation (Eden + Survivor spaces).
  • Happens frequently because most objects in Java are short-lived.
  • Fast collection since most objects in Young Generation die young.

How Minor GC Works:

  1. New objects are created in Eden space.
  2. When Eden fills up, a Minor GC is triggered.
  3. Live objects in Eden are moved to Survivor space (S0/S1).
  4. Objects surviving multiple Minor GCs are promoted to the Old Generation.
  5. Unreachable objects are removed.

Example of Minor GC:

for (int i = 0; i < 1000000; i++) {
    String temp = new String("Hello"); // Created in Eden
} // Once Eden fills, Minor GC runs
  • temp objects are short-lived and collected in Minor GC.

Key Features of Minor GC:

Quick collection due to short-lived objects.
Triggers automatically when Eden fills up.
Does not cause long application pauses.
Promotes surviving objects to Old Generation if they last long enough.

2️⃣ Major GC (Old Generation Collection)

What is Major GC?

  • A Major GC (also called Full GC) is a garbage collection event that cleans the Old Generation.
  • Less frequent than Minor GC.
  • Takes more time because Old Generation contains long-lived objects.
  • Can cause “Stop-the-World” pauses (all application threads halt).

How Major GC Works:

  1. Old Generation fills up as objects are promoted from the Young Generation.
  2. JVM triggers a Major GC to clean unused objects in the Old Generation.
  3. If fragmentation occurs, JVM compacts memory (especially in Serial GC and G1GC).
  4. If there is not enough space after GC, the application may crash with OutOfMemoryError.

Example of Major GC:

List<byte[]> memory = new ArrayList<>();
while (true) {
    memory.add(new byte[10_000_000]); // Large objects go directly to Old Generation
}
  • The heap fills up, triggering Major GC.

Key Features of Major GC:

Collects long-lived objects from Old Generation.
Less frequent but more expensive than Minor GC.
Can cause longer pause times.
Can trigger “Stop-the-World” events.
Might lead to OutOfMemoryError if memory cannot be freed.


3️⃣ Full GC (Major GC + Minor GC)

What is Full GC?

  • A Full GC is a Major GC that also cleans the Young Generation.
  • Triggers when the heap is almost full or when explicit GC (System.gc()) is called.
  • Most expensive type of GC because it scans the entire heap.
  • Can cause significant Stop-the-World pauses.

When Does Full GC Happen?

🔸 When Old Generation is full and objects cannot be promoted.
🔸 When GC tuning parameters force full compaction (e.g., -XX:+UseSerialGC).
🔸 When System.gc() is called (not recommended).
🔸 When GC algorithms like G1GC decide a Full GC is necessary.

How to Avoid Full GC?

🚀 Use region-based collectors like G1GC, ZGC, Shenandoah (which avoid Full GC by concurrent collection).
🚀 Increase heap size (-Xmx option) to reduce GC frequency.
🚀 Optimize object lifecycle (avoid long-lived temporary objects).


4️⃣ Summary: Minor GC vs. Major GC

FeatureMinor GCMajor GC
CleansYoung Generation (Eden, Survivor)Old Generation
FrequencyFrequentInfrequent
SpeedFast (short pauses)Slow (long pauses)
Pause TypeShort Stop-the-WorldLong Stop-the-World
CauseEden fills upOld Generation fills up
ImpactLow latencyCan freeze application
Leads toPromotion to Old GenFull GC if memory runs out
Triggers Full GC?❌ No✅ Yes, if memory is exhausted

📌 In short:
🔹 Minor GC cleans short-lived objects quickly (low impact).
🔹 Major GC cleans long-lived objects but can cause long pauses.
🔹 Full GC is the worst-case scenario (both Young & Old are scanned).

🚀 Optimizing GC helps reduce long pauses and improve Java performance!

This entry was posted in Без рубрики. Bookmark the permalink.