Java / Java 17 Garbage Collection Interview Questions
What are Survivor spaces used for?
The two survivor spaces, S0 and S1, hold objects that have survived at least one minor GC but haven't yet been promoted to the old generation. Only one of the two is "active" (in use) at any given time; the other stays empty until the next collection.
During each minor GC, live objects are copied from Eden and the currently active survivor space into the other, currently-empty survivor space - this copying compaction is what keeps the young generation free of fragmentation. Each surviving object's age counter increments on every copy.
Once an object's age reaches -XX:MaxTenuringThreshold (default 15, and often adjusted dynamically based on survivor occupancy), or if a survivor space fills up before that threshold is hit, the object is promoted into the old generation instead of being copied again.
More Related questions...