Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/org/javademos/init/Java12DemoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.javademos.java12.jep334.ConstantsAPIDemo;
import org.javademos.java12.jep340.OneAArch64Port;
import org.javademos.java12.jep341.DefaultCDSArchives;
import org.javademos.java12.jep344.AbortableMixedCollectionsG1;

/**
* Loads demos for Java 12.
Expand All @@ -25,5 +26,6 @@ public void loadDemos(Map<Integer, IDemo> demos) {
demos.put(334, new ConstantsAPIDemo());
demos.put(340, new OneAArch64Port());
demos.put(341, new DefaultCDSArchives());
demos.put(344, new AbortableMixedCollectionsG1());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.javademos.java12.jep344;

import java.lang.management.ManagementFactory;

import org.javademos.commons.IDemo;

/// Demo for JDK 12 feature JEP 344 - Abortable Mixed Collections for G1.
///
/// JEP history:
/// - JDK 12: [JEP 344 - Abortable Mixed Collections for G1](https://openjdk.org/jeps/344)
///
/// Further reading:
/// - [JDK-8190269 - JEP 344: Abortable Mixed Collections for G1](https://bugs.openjdk.org/browse/JDK-8190269)
/// - [Garbage-First (G1) Garbage Collector tuning guide](https://docs.oracle.com/en/java/javase/21/gctuning/garbage-first-g1-garbage-collector1.html)
///
/// @author alois.seckar@gmail.com
public class AbortableMixedCollectionsG1 implements IDemo {
@Override
public void demo() {
info(344);

// This JEP is an internal improvement of the G1 garbage collector (default since JDK 9),
// so there is no new API to call from application code.

// G1 tries to meet a user-supplied pause time target (`-XX:MaxGCPauseMillis`, 200 ms by default).
// Before each pause it uses heuristics to pick a "collection set" - the heap regions to collect.
// Once started, all live objects in that set had to be evacuated without stopping.
// If heuristics worked with "stale" data (e.g. application behavior changed), the set
// could be too large and the pause exceeded the target. This mostly happened during
// "mixed" collections, which collect young regions plus some old regions.

// Since JDK 12, when G1 detects it repeatedly picks too much work, it splits
// the collection set into two parts:
// - mandatory - must be collected at once (young regions, possibly some old regions)
// - optional - only old regions (e.g. ~20 % of the predicted set)
// After the mandatory part is done, G1 processes the optional part in smaller
// increments (down to a single region) and may stop ("abort") whenever the remaining
// pause time runs out. Space reclamation still progresses region by region.
// As predictions become accurate again, the optional part shrinks until G1
// fully relies on its heuristics once more.

// The mechanism is automatic and needs no flags; it only applies when G1 is in use.
// Collector details are visible via `-Xlog:gc+phases=debug`.
System.out.println("Active garbage collectors:");
ManagementFactory.getGarbageCollectorMXBeans()
.forEach(gc -> System.out.println("- " + gc.getName()));
}
}
8 changes: 8 additions & 0 deletions src/main/resources/JDK12Info.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@
"dscr": "The JDK build now generates a default class data-sharing (CDS) archive on 64-bit platforms, improving out-of-the-box startup time.",
"link": false,
"code": true
},
{
"jep": 344,
"jdk": 12,
"name": "JEP 344 - Abortable Mixed Collections for G1",
"dscr": "G1 can split mixed collections into mandatory and optional parts and abort the optional part to better meet the pause time target.",
"link": false,
"code": true
}
]
Loading