Skip to lesson

Minor, mixed and full collections

Not every garbage collection does the same amount of work. HotSpot's G1 collector runs young collections most of the time, mixed collections when the old generation needs cleaning, and a full GC only when it cannot keep up. Telling them apart in a log is the first step in understanding a slow Java program.

Young (minor) collections and why they are cheap

A young (minor) GC collects only the young generation: eden and the survivor spaces. It is usually short, because most objects there are already dead.

A young collection, also called a minor GC, collects only the young generation: eden and the survivor spaces. Live young objects are copied to a survivor space or promoted, and all of eden becomes free.

Example 1 · One program for the whole lesson: delivery sessions

DeliverySessionsDemo.javaJava
import java.util.ArrayDeque;import java.util.Deque;
public class DeliverySessionsDemo {    public static void main(String[] args) {        Deque<byte[]> activeSessions = new ArrayDeque<>();        for (int i = 0; i < 200_000; i++) {            activeSessions.addLast(new byte[2_000]);   // a 2 KB delivery session            if (activeSessions.size() > 10_000) {        // keep the latest 10,000 (about 20 MB)                activeSessions.removeFirst();            // the oldest session ends            }        }        System.out.println("Sessions in memory: " + activeSessions.size());    }}
Output
Sessions in memory: 10000

What this code does

  • Each session is a 2 KB array that stays alive while the next 10,000 sessions are created, then becomes garbage. About 20 MB is always live, and 400 MB is allocated in total.
  • Sessions live long enough to survive a young collection, but not forever. The next examples run this same program with different heap sizes and -Xlog:gc to see all three kinds of collections.

Example 2 · Young collections with the default heap

Terminal
java -Xlog:gc DeliverySessionsDemo
Sample output
[0.019s][info][gc] Using G1
[0.175s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->20M(118M) 12.240ms
[0.189s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 34M->32M(118M) 8.982ms
[0.210s][info][gc] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 56M->49M(118M) 11.711ms
[0.229s][info][gc] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 70M->65M(118M) 10.741ms
...

What this code does

  • Every line is a young collection: Pause Young (Normal). Each one paused the program for about 9 to 12 ms (HotSpot, Java 21, default heap of 118 MB to start).
  • Notice 23M->20M: most young objects here survived, so these pauses are longer than usual. The used heap also keeps rising, which is the first hint of premature promotion (section 4).

It is cheap for three reasons. The young generation is small, most of its objects are already dead, and copying costs time only for the live ones. In a G1 log a young collection shows as Pause Young (Normal). The program is paused for it, usually for a few milliseconds.

G1 mixed collections that also clean old regions

A mixed collection is a G1 young collection that also evacuates some old regions. G1 picks the old regions that hold the most garbage, because they free the most memory for the least copying. This is where the name Garbage-First comes from.

Example 3 · Concurrent marking, then mixed collections

Terminal
java -Xlog:gc DeliverySessionsDemo
Sample output
...
[0.247s][info][gc] GC(4) Pause Young (Concurrent Start) (G1 Evacuation Pause) 80M->77M(354M) 11.532ms
[0.247s][info][gc] GC(5) Concurrent Mark Cycle
[0.300s][info][gc] GC(5) Pause Remark 153M->98M(99M) 0.809ms
[0.310s][info][gc] GC(5) Pause Cleanup 98M->98M(99M) 0.127ms
[0.312s][info][gc] GC(5) Concurrent Mark Cycle 64.325ms
[0.325s][info][gc] GC(6) Pause Young (Prepare Mixed) (G1 Evacuation Pause) 98M->39M(119M) 13.800ms
[0.347s][info][gc] GC(7) Pause Young (Mixed) (G1 Evacuation Pause) 70M->47M(119M) 8.076ms
[0.366s][info][gc] GC(8) Pause Young (Normal) (G1 Evacuation Pause) 71M->61M(119M) 7.994ms
...

What this code does

  • The same run, a little later. Concurrent Start begins marking the old generation; the Concurrent Mark Cycle took 64 ms in total, but the program paused only for the short Remark (0.8 ms) and Cleanup (0.1 ms) steps.
  • Then Prepare Mixed and Pause Young (Mixed) evacuate young regions plus the old regions with the most dead sessions: used memory fell from 98 MB to 39 MB. No full GC was needed.

G1 first has to know how much garbage each old region holds, so it runs concurrent marking: marking the old generation while your program keeps running. It starts when the old generation passes a fill level (the initiating heap occupancy, 45% of the heap by default on Java 17 and 21, adjusted automatically as the program runs). In the log the cycle reads: Concurrent Start, Concurrent Mark Cycle, Remark, Cleanup, Prepare Mixed, and then one or more Pause Young (Mixed).

Full GC and why it is a warning sign

A full GC collects the whole heap, young and old together, in one long stop-the-world pause. In G1 it shows as Pause Full, and since Java 10 it uses several threads, but it still pauses the program for the entire job.

Example 4 · A small heap: evacuation failure and a full GC

Terminal
java -Xmx64m -Xlog:gc DeliverySessionsDemo
Sample output
[0.018s][info][gc] Using G1
[0.181s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->20M(64M) 13.625ms
[0.193s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 32M->30M(64M) 8.263ms
[0.204s][info][gc] GC(2) Pause Young (Concurrent Start) (G1 Evacuation Pause) 39M->40M(64M) 6.770ms
[0.204s][info][gc] GC(3) Concurrent Mark Cycle
...
[0.227s][info][gc] GC(9) Pause Young (Normal) (G1 Evacuation Pause) (Evacuation Failure) 61M->62M(64M) 1.248ms
[0.228s][info][gc] GC(10) Pause Young (Normal) (G1 Evacuation Pause) (Evacuation Failure) 63M->64M(64M) 0.697ms
[0.234s][info][gc] GC(11) Pause Full (G1 Compaction Pause) 64M->19M(64M) 5.868ms
[0.234s][info][gc] GC(3) Concurrent Mark Cycle 30.034ms
...

What this code does

  • With a 64 MB limit, the heap filled up before concurrent marking finished. G1 had no free regions to copy into (Evacuation Failure), so it fell back to Pause Full (G1 Compaction Pause): the whole heap, compacted in one pause.
  • With -Xmx32m the same program ran 59 full GCs out of 238 pauses, each leaving about 19 MB live in 32 MB. Full GCs that keep returning, with a high "after" value, mean the heap is too small for the live data, or something is leaking.

For G1 a full GC is a fallback. It happens when G1 runs out of free regions before its normal young and mixed collections can make room, for example after an Evacuation Failure (no free space to copy live objects into), or when the code calls System.gc(). Frequent full GCs usually mean the live data hardly fits in the heap, there is a memory leak, or objects are promoted too fast. The Serial and Parallel collectors are different: for them a full collection is the normal way to clean the old generation. People often say major GC for an old-generation collection; the term is informal and the JVM logs do not use it.

Premature promotion

Premature promotion happens when objects that live a medium time are promoted to the old generation and then die there soon after. They should have died young, but young collections can no longer free them; only a mixed or full collection can.

Example 5 · Premature promotion in the log

Terminal
java -Xlog:gc,gc+heap=info,gc+age=debug DeliverySessionsDemo
Sample output
[0.018s][info][gc] Using G1
[0.183s][debug][gc,age] GC(0) Desired survivor size 1572864 bytes, new threshold 15 (max threshold 15)
[0.200s][info ][gc,heap] GC(0) Eden regions: 23->0(14)
[0.200s][info ][gc,heap] GC(0) Survivor regions: 0->3(3)
[0.200s][info ][gc,heap] GC(0) Old regions: 0->18
[0.200s][info ][gc,heap] GC(0) Humongous regions: 0->0
[0.200s][info ][gc     ] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->20M(118M) 17.364ms
[0.206s][debug][gc,age ] GC(1) Desired survivor size 1572864 bytes, new threshold 1 (max threshold 15)
[0.215s][info ][gc,heap] GC(1) Eden regions: 14->0(24)
[0.215s][info ][gc,heap] GC(1) Survivor regions: 3->3(3)
[0.215s][info ][gc,heap] GC(1) Old regions: 18->30
...

What this code does

  • The very first young GC found 20 MB of live sessions, but the survivor space had room for only 3 regions. The other 18 regions' worth was promoted at age 1: Old regions: 0->18.
  • At GC(1) the JVM lowered the tenuring threshold to 1, and old regions keep growing (18 → 30 → ...). These sessions die a few milliseconds later, but now only the mixed collections from Example 3 can free them.

Example 6 · Simulation: a small survivor space promotes too early

PrematurePromotionDemo.javaJava
import java.util.ArrayList;import java.util.List;
class Session {    int livesFor;    int age = 0;
    Session(int livesFor) {        this.livesFor = livesFor;    }}
public class PrematurePromotionDemo {    static int run(int survivorCapacity) {        List<Session> survivor = new ArrayList<>();        int promotedEarly = 0;        for (int gc = 1; gc <= 6; gc++) {            List<Session> live = new ArrayList<>(survivor);   // older objects get room first            for (int k = 0; k < 4; k++) {                live.add(new Session(2));                     // 4 new sessions per round            }            List<Session> to = new ArrayList<>();            for (Session s : live) {                if (s.age >= s.livesFor) {                    continue;                }                s.age++;                if (to.size() < survivorCapacity) {                    to.add(s);                } else {                    promotedEarly++;                          // no room: promoted to old                }            }            survivor = to;        }        return promotedEarly;    }
    public static void main(String[] args) {        for (int capacity : new int[] {4, 8}) {            System.out.println("Survivor room for " + capacity + " objects: "                    + run(capacity) + " of 24 sessions promoted early");        }    }}
Output
Survivor room for 4 objects: 12 of 24 sessions promoted early
Survivor room for 8 objects: 0 of 24 sessions promoted early

What this code does

  • A simplified model: every round creates 4 sessions that each survive 2 young GCs. With room for 4, the second-round survivors and the new ones do not fit together, so half of all sessions are pushed into the old generation while still alive.
  • With room for 8, every session dies in the young generation and costs nothing more. The promoted ones in the first case all die in the old generation soon after, where only a mixed or full collection can free them.

The usual causes are a survivor space too small for the objects that survive one collection, so the overflow is promoted at once, and the JVM lowering the tenuring threshold to 1 for the same reason. In the log it shows as old regions growing at every young GC and new threshold 1 in the age lines. The effect is more mixed collections, and in a small heap full GCs. The usual fixes are fewer medium-lived objects, or more room for the young generation; the next module covers the heap options.

Remembered sets in simple terms

A young collection must find every live young object, including ones that only an old object points to. Scanning the whole old generation for such references would make young collections slow.

Java
class OrderHistory {    static List<Order> all = new ArrayList<>();   // long-lived: sits in old regions
    static void record(String id) {        Order o = new Order(id);                  // new object: in eden        all.add(o);                               // old -> young reference: the write    }                                             // barrier records it in a remembered set}

A remembered set solves this: it records where references from other regions point into a region.

The JVM keeps it up to date with a write barrier: a few extra machine instructions the JIT adds to every store into a reference field. When the code stores a reference into an object that lives in another region, the barrier records the spot (a small block of memory called a card, 512 bytes in HotSpot). At a young collection those recorded old-to-young references are treated as extra roots, so only the recorded cards are scanned, not the whole old generation.

Simplified: G1 keeps one remembered set per region and also filters out references that cannot matter. The idea, "remember old-to-young references so young GCs stay small", is the same in every generational collector.

Next step

Check your understanding

12 quick questions