Master the fundamental concepts of jvm internals through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksG1 (Garbage-First) divides the heap into equal-sized regions and collects the ones with the most garbage first. It targets predictable pause times on large heaps, unlike single-heap stop-the-world collectors.
Key mechanisms:
Young collections evacuate live objects from eden regions; mixed collections fold in old regions with high reclaim potential.
For example, a 8 GiB heap split into 2048 regions lets G1 target a 200 ms pause by collecting just 20 regions worth of garbage.
Pause time goals are hints, not guarantees. Under allocation pressure G1 may still collect more regions than planned; pair `-XX:MaxGCPauseMillis` with GC logs to see real behavior.
This exercise asks you to document G1's region model, remembered sets, and SATB write barriers. You will explain how G1 bounds pause time compared to whole-heap collectors.
You will use the same mental model here when reading production interpreter source later in the track. Sketch one concrete input on paper, predict the outcome, then confirm with code. That discipline catches logic errors early and makes debugging far faster when you extend the implementation in follow-on tasks.
Simulate the G1 garbage collector on a tiny heap:
16 regions (r0-r15) of 8 units each. Objects have a size in units, two reference fields (0 and 1, initially null), an age, and a root flag.
eden full: young GC and run a young GC labelled young GC (allocation failure).humongous, then humongous-cont, with the last one holding the remainder). They never move.ref A.F B) SATB: enqueue OLD and remember it. RSet[rB] += rA and add A's region to the RSet of B's region. eager reclaim: humongous NAME (rX-rY).The summary is LABEL: N live copied (S to survivor, P to old), D dead, F region(s) freed.
mark start: clears all marks and prints concurrent mark started.
mark end: marks everything reachable from the current roots, from every SATB-enqueued object, and from every object allocated during the cycle. Then it prints concurrent mark finished: and rN old: L/U units live for each old region.
mixed-gc (needs a finished marking, else mixed-gc: needs a completed marking cycle) collects:
collection set += rN (L/U live).Old objects in the collection set survive if they are marked, and move to old regions. The summary is labelled mixed GC.
regions lists the non-free regions: rN TYPE USED/8: names (- if empty, nothing for humongous-cont), then rset {rA rB} if the RSet is non-empty, then K free regions.root NAME / unroot NAME.Errors:
alloc: NAME SIZE (1-32 units)alloc X: name in usealloc X: out of memory…root: no object X / unroot: no object Xref: OBJ.FIELD (0-1) TARGET|nullref: no object Xmark: start|end, mark: already marking, mark: not markingunknown command XInput:
cLoading…
Output:
cLoading…
Hidden tests cover SATB keeping an overwritten object alive through a mixed GC, the choice of the mixed collection set, eden-full allocation failure, name reuse after collection, and every command error.