@@ -55,7 +55,7 @@ Starting in version 3.13, CPython contains two GC implementations:
5555 performing a collection for thread safety.
5656
5757Both implementations use the same basic algorithms, but operate on different
58- data structures. See the section on
58+ data structures. See the section on
5959[ Differences between GC implementations] ( #Differences-between-GC-implementations )
6060for the details.
6161
@@ -64,7 +64,7 @@ Memory layout and object structure
6464==================================
6565
6666The garbage collector requires additional fields in Python objects to support
67- garbage collection. These extra fields are different in the default and the
67+ garbage collection. These extra fields are different in the default and the
6868free-threaded builds.
6969
7070
@@ -111,11 +111,11 @@ that in the [Optimization: incremental collection](#Optimization-incremental-col
111111they are also reused to fulfill other purposes when the full doubly linked list
112112structure is not needed as a memory optimization.
113113
114- Doubly linked lists are used because they efficiently support the most frequently required operations. In
114+ Doubly linked lists are used because they efficiently support the most frequently required operations. In
115115general, the collection of all objects tracked by GC is partitioned into disjoint sets, each in its own
116- doubly linked list. Between collections, objects are partitioned into "generations", reflecting how
117- often they've survived collection attempts. During collections, the generation(s) being collected
118- are further partitioned into, for example, sets of reachable and unreachable objects. Doubly linked lists
116+ doubly linked list. Between collections, objects are partitioned into "generations", reflecting how
117+ often they've survived collection attempts. During collections, the generation(s) being collected
118+ are further partitioned into, for example, sets of reachable and unreachable objects. Doubly linked lists
119119support moving an object from one partition to another, adding a new object, removing an object
120120entirely (objects tracked by GC are most often reclaimed by the refcounting system when GC
121121isn't running at all!), and merging partitions, all with a small constant number of pointer updates.
@@ -128,7 +128,7 @@ GC for the free-threaded build
128128In the free-threaded build, Python objects contain a 1-byte field
129129` ob_gc_bits ` that is used to track garbage collection related state. The
130130field exists in all objects, including ones that do not support cyclic
131- garbage collection. The field is used to identify objects that are tracked
131+ garbage collection. The field is used to identify objects that are tracked
132132by the collector, ensure that finalizers are called only once per object,
133133and, during garbage collection, differentiate reachable vs. unreachable objects.
134134
@@ -192,7 +192,7 @@ the interpreter create cycles everywhere. Some notable examples:
192192 have internal links to themselves.
193193
194194To correctly dispose of these objects once they become unreachable, they need
195- to be identified first. To understand how the algorithm works, let’s take
195+ to be identified first. To understand how the algorithm works, let’s take
196196the case of a circular linked list which has one link referenced by a
197197variable ` A ` , and one self-referencing object which is completely
198198unreachable:
@@ -220,15 +220,15 @@ unreachable:
2202202
221221```
222222
223- The GC starts with a set of candidate objects it wants to scan. In the
223+ The GC starts with a set of candidate objects it wants to scan. In the
224224default build, these "objects to scan" might be all container objects or a
225- smaller subset (or "generation"). In the free-threaded build, the collector
225+ smaller subset (or "generation"). In the free-threaded build, the collector
226226always scans all container objects.
227227
228- The objective is to identify all the unreachable objects. The collector does
228+ The objective is to identify all the unreachable objects. The collector does
229229this by identifying reachable objects; the remaining objects must be
230- unreachable. The first step is to identify all of the "to scan" objects that
231- are ** directly** reachable from outside the set of candidate objects. These
230+ unreachable. The first step is to identify all of the "to scan" objects that
231+ are ** directly** reachable from outside the set of candidate objects. These
232232objects have a refcount larger than the number of incoming references from
233233within the candidate set.
234234
@@ -241,7 +241,7 @@ interpreter will not modify the real reference count field.
241241![ gc-image1] ( images/python-cyclic-gc-1-new-page.png )
242242
243243The GC then iterates over all containers in the first list and decrements by one the
244- ` gc_ref ` field of any other object that container is referencing. Doing
244+ ` gc_ref ` field of any other object that container is referencing. Doing
245245this makes use of the ` tp_traverse ` slot in the container class (implemented
246246using the C API or inherited by a superclass) to know what objects are referenced by
247247each container. After all the objects have been scanned, only the objects that have
@@ -273,7 +273,7 @@ When the GC encounters an object which is reachable (`gc_ref > 0`), it traverses
273273its references using the ` tp_traverse ` slot to find all the objects that are
274274reachable from it, moving them to the end of the list of reachable objects (where
275275they started originally) and setting its ` gc_ref ` field to 1. This is what happens
276- to ` link_2 ` and ` link_3 ` below as they are reachable from ` link_1 ` . From the
276+ to ` link_2 ` and ` link_3 ` below as they are reachable from ` link_1 ` . From the
277277state in the previous image and after examining the objects referred to by ` link_1 `
278278the GC knows that ` link_3 ` is reachable after all, so it is moved back to the
279279original list and its ` gc_ref ` field is set to 1 so that if the GC visits it again,
@@ -293,7 +293,7 @@ list are really unreachable and can thus be garbage collected.
293293
294294Pragmatically, it's important to note that no recursion is required by any of this,
295295and neither does it in any other way require additional memory proportional to the
296- number of objects, number of pointers, or the lengths of pointer chains. Apart from
296+ number of objects, number of pointers, or the lengths of pointer chains. Apart from
297297` O(1) ` storage for internal C needs, the objects themselves contain all the storage
298298the GC algorithms require.
299299
@@ -317,8 +317,8 @@ list.
317317So instead of not moving at all, the reachable objects B and A are each moved twice.
318318Why is this a win? A straightforward algorithm to move the reachable objects instead
319319would move A, B, and C once each. The key is that this dance leaves the objects in
320- order C, B, A - it's reversed from the original order. On all * subsequent* scans,
321- none of them will move. Since most objects aren't in cycles, this can save an
320+ order C, B, A - it's reversed from the original order. On all * subsequent* scans,
321+ none of them will move. Since most objects aren't in cycles, this can save an
322322unbounded number of moves across an unbounded number of later collections. The only
323323time the cost can be higher is the first time the chain is scanned.
324324
@@ -331,7 +331,7 @@ follows these steps in order:
331331
3323321 . Handle and clear weak references (if any). Weak references to unreachable objects
333333 are set to ` None ` . If the weak reference has an associated callback, the callback
334- is enqueued to be called once the clearing of weak references is finished. We only
334+ is enqueued to be called once the clearing of weak references is finished. We only
335335 invoke callbacks for weak references that are themselves reachable. If both the weak
336336 reference and the pointed-to object are unreachable we do not execute the callback.
337337 This is partly for historical reasons: the callback could resurrect an unreachable
@@ -490,7 +490,7 @@ to the size of the data, often a word or multiple thereof. This discrepancy
490490leaves a few of the least significant bits of the pointer unused, which can be
491491used for tags or to keep other information – most often as a bit field (each
492492bit a separate tag) – as long as code that uses the pointer masks out these
493- bits before accessing memory. For example, on a 32-bit architecture (for both
493+ bits before accessing memory. For example, on a 32-bit architecture (for both
494494addresses and word size), a word is 32 bits = 4 bytes, so word-aligned
495495addresses are always a multiple of 4, hence end in ` 00 ` , leaving the last 2 bits
496496available; while on a 64-bit architecture, a word is 64 bits = 8 bytes, so
@@ -519,10 +519,10 @@ of `PyGC_Head` discussed in the `Memory layout and object structure`_ section:
519519- The ` _gc_next ` field is used as the "next" pointer to maintain the doubly linked
520520 list but during collection its lowest bit is used to keep the
521521 ` NEXT_MASK_UNREACHABLE ` flag that indicates if an object is tentatively
522- unreachable during the cycle detection algorithm. This is a drawback to using only
522+ unreachable during the cycle detection algorithm. This is a drawback to using only
523523 doubly linked lists to implement partitions: while most needed operations are
524524 constant-time, there is no efficient way to determine which partition an object is
525- currently in. Instead, when that's needed, ad hoc tricks (like the
525+ currently in. Instead, when that's needed, ad hoc tricks (like the
526526 ` NEXT_MASK_UNREACHABLE ` flag) are employed.
527527
528528Optimization: delayed untracking containers
@@ -581,29 +581,29 @@ structure, while the free-threaded build implementation does not use that
581581data structure.
582582
583583- The default build implementation stores all tracked objects in a doubly
584- linked list using ` PyGC_Head ` . The free-threaded build implementation
584+ linked list using ` PyGC_Head ` . The free-threaded build implementation
585585 instead relies on the embedded mimalloc memory allocator to scan the heap
586586 for tracked objects.
587587- The default build implementation uses ` PyGC_Head ` for the unreachable
588- object list. The free-threaded build implementation repurposes the
588+ object list. The free-threaded build implementation repurposes the
589589 ` ob_tid ` field to store a unreachable objects linked list.
590590- The default build implementation stores flags in the ` _gc_prev ` field of
591- ` PyGC_Head ` . The free-threaded build implementation stores these flags
591+ ` PyGC_Head ` . The free-threaded build implementation stores these flags
592592 in ` ob_gc_bits ` .
593593
594594
595595The default build implementation relies on the
596596[ global interpreter lock] ( https://docs.python.org/3/glossary.html#term-global-interpreter-lock )
597- for thread safety. The free-threaded build implementation has two "stop the
597+ for thread safety. The free-threaded build implementation has two "stop the
598598world" pauses, in which all other executing threads are temporarily paused so
599599that the GC can safely access reference counts and object attributes.
600600
601- The default build implementation is a generational collector. The
601+ The default build implementation is a generational collector. The
602602free-threaded build is non-generational; each collection scans the entire
603603heap.
604604
605605- Keeping track of object generations is simple and inexpensive in the default
606- build. The free-threaded build relies on mimalloc for finding tracked
606+ build. The free-threaded build relies on mimalloc for finding tracked
607607 objects; identifying "young" objects without scanning the entire heap would
608608 be more difficult.
609609
0 commit comments