What is a 'single-generation' garbage collector in Java?
Asked Answered
S

2

5

In reading about ZGC I notice that it brags about being a 'single-generation garbage collector,' but I rarely see any details on exactly what this term means.

Normal Generational GC

I'm familiar with Eden, the survivor space, the nursery, hospice care, metaspace, permgen, zombified objects, Noah's Ark and the old age home, so I don't need an explanation of how concurrent mark sweep (CMS GC) or the garbage first (G1 GC) algorithms work. My understanding is those are both multi-generational, and I'm good with that.

Personally, I always liked being able to go through Java Mission Control and see how many generations of GC cycles an object has lived through. That was always helpful in troubleshooting a memory leak or GC problem.

Single vs Multi-Generational GC

So what exactly is a 'single-generation' garbage collector, and how does that differ from how objects are currently tracked through multiple garbage collection cycles in CMS and G1?

Serrano answered 24/12, 2020 at 15:5 Comment(1)
S
5

tl;dr

ZGC has much to brag about, but being single-generation is not among that.

Adding multi-generational is planned as future improvement.

Details

Some garbage collector implementations distinguish between new objects and old objects, the “young generation” versus “old generation”. The goal is to make it cheaper for the programmer to create short-lived objects as they will be more quickly and efficiently disposed of to free up memory.

ZGC does not “brag” about being single-generational. Quite the opposite, the team has indicated their desire to add generational features in future versions of ZGC. See a discussion in this section of a talk by Per Liden of Oracle (wait a few seconds for new slide “Generational ZGC”): https://youtu.be/88E86quLmQA

For more info on generations, see this Question: Java heap terminology: young, old and permanent generations?

Stereoscopic answered 24/12, 2020 at 16:3 Comment(2)
Was just trying to be colorful in my speech with the term 'brag.'Serrano
@CameronMcKenzie Understood. ZGC has much to brag about, but being single-generation is not one of those.Stereoscopic
R
3

You do know the details, but do you know why generational garbage collectors were invented? Specifically, why G1GC is generational?

The idea at the time was that scanning the entire heap was expensive, were expensive meant STW. G1 as a matter of fact started its life a non-generational (or single-generation as you call it), but the size of remembered sets and their time to go over them, triggered it to become generational. The fact that there are "generations" + the fact that G1 scans all the young regions in a single cycle (or better said if it "committed" to scan some X number of young regions at the beginning of some cycle - it must scan them all), means that you do not need for remembered sets to have connections between them in the young space (also card table is a lot smaller).

As to you direct question, ZGC (as well as Shenandoah) do not "track" how many cycles an Object has survived, they do not need to do that, as they have no generations, so no need to move Objects somewhere in the "old" to be scanned "later". They scan the entire heap on each cycle. They do this concurrently (in a very interesting way), so there is simply no need, at the moment, for them to be generational.

Ransom answered 28/12, 2020 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.