Skip to content

Commit f5c6759

Browse files
committed
Further clarifications
1 parent a689b38 commit f5c6759

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

InternalDocs/garbage_collector.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ follows these steps in order:
354354
Optimization: incremental collection
355355
====================================
356356

357-
In order to limit the time each garbage collection takes, the GC implementation
357+
In order to bound the length of each garbage collection pause, the GC implementation
358358
for the default build uses incremental collection with two generations.
359359

360360
Generational garbage collection takes advantage of what is known as the weak
@@ -364,9 +364,14 @@ programs as many temporary objects are created and destroyed very quickly.
364364

365365
To take advantage of this fact, all container objects are segregated into
366366
two generations: young and old. Every new object starts in the young generation.
367+
Each garbage collection scans the entire young generation and part of the old generation.
368+
369+
The time taken to scan the young generation can be controlled by controlling the
370+
size of the young, but the size of the old generation cannot be controlled.
367371
In order to keep pause times down, scanning of the old generation of the heap
368-
occurs in increments. To keep track of what has been scanned,
369-
the old generation contains two lists:
372+
occurs in increments.
373+
374+
To keep track of what has been scanned, the old generation contains two lists:
370375

371376
* Those objects that have not yet been scanned, referred to as the `pending` list.
372377
* Those objects that have been scanned, referred to as the `visited` list.
@@ -400,17 +405,21 @@ The `visited` and `pending` lists can be swapped by toggling this bit.
400405
Correctness
401406
-----------
402407

403-
In order to collect all unreachable cycles, each increment must contain all of
404-
an unreachable cycle, or none of it.
405-
In order to make sure that the whole of any unreachable cycle is contained in an
406-
increment, all unscanned objects reachable from any object in the increment must
407-
be included in the increment.
408-
Thus, to form a complete increment we perform a
408+
The [algorithm for identifying cycles](#Identifying-reference-cycles) will find all
409+
unreachable cycles in a list of objects, but will not find any cycles that are
410+
even partly outside of that list.
411+
Therefore, to be guaranteed that a full scavenge will find all unreachable cycles,
412+
each cycle must be fully contained within a single increment.
413+
414+
To make sure that no partial cycles are included in the increment we perform a
409415
[transitive closure](https://en.wikipedia.org/wiki/Transitive_closure)
410416
over reachable, unscanned objects from the initial increment.
417+
Since the transitive closure of objects reachable from an object must be a (non-strict)
418+
superset of any unreachable cycle including that object, we are guaranteed that a
419+
transitive closure cannot contain any partial cycles.
411420
We can exclude scanned objects, as they must have been reachable when scanned.
412-
If a scanned object becomes part of an unreachable cycle after being scanned, it
413-
will not be collected this cycle, but it will be collected in the next full scavenge.
421+
If a scanned object becomes part of an unreachable cycle after being scanned, it will
422+
not be collected this at this time, but it will be collected in the next full scavenge.
414423

415424
> [!NOTE]
416425
> The GC implementation for the free-threaded build does not use incremental collection.

0 commit comments

Comments
 (0)