@@ -270,3 +270,27 @@ counting are not immediately freed when their internal reference count goes to
270270zero. Instead, they are examined by the next GC run and, if no stack
271271references to them are found, they are freed. This means these objects are
272272freed by the GC and not when their reference count goes to zero, as is typical.
273+
274+
275+ Per-thread reference counting can delay freeing objects
276+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
277+
278+ To avoid contention on the reference count fields of frequently shared
279+ objects, the free-threaded build also uses "per-thread reference counting"
280+ for a few selected object types. Rather than updating a single shared
281+ reference count, each thread maintains its own local reference count array,
282+ indexed by a unique id assigned to the object. The true reference count is
283+ only computed by summing the per-thread counts when the object's local
284+ count drops to zero. Per-thread reference counting is currently used for:
285+
286+ * heap type objects (classes created in Python)
287+ * code objects
288+ * the ``__dict__ `` of module objects
289+
290+ Because the per-thread counts must be merged back to the object before it
291+ can be deallocated, objects using per-thread reference counting are
292+ typically freed later than they would be in the default build. In
293+ particular, such an object is usually not freed until the thread that
294+ referenced it reaches a safe point (for example, in the "eval breaker"
295+ section of the bytecode evaluator) or exits. Running :func: `gc.collect `
296+ will merge the per-thread counts and allow these objects to be freed.
0 commit comments