Commit Graph
74 Commits
Author SHA1 Message Date
Victor StinnerandGitHub 0036565e81 gh-146527: Fix memory leak in _PyGC_Fini() (#150969)
Free generation_stats allocated by _PyGC_Init().

Fix Python/gc.c: Python/gc_free_threading.c was already fixed.
2026-06-05 13:43:05 +00:00
Sergey MiryanovandGitHub e89568f0cb GH-148726: Add heap_size to generational GC (#149195) 2026-05-04 22:14:45 +01:00
sobolevnandGitHub 98afa03522 gh-149282: Fix gc.c compilation with --enable-pystats (#149283) 2026-05-02 18:24:17 +03:00
Sergey MiryanovandGitHub 39f123c587 GH-146527: Add get_gc_stats function to _remote_debugging (#148071) 2026-05-02 15:04:18 +00:00
1575a81bf2 GH-148726: Forward-port generational GC. (GH-148746)
The replaces the incremental GC with a forward port (from 3.13) of the generational GC.

Co-Authored-By: Neil Schemenauer <[email protected]>
Co-Authored-By: Zanie Blue <[email protected]>
Co-Authored-By: Sergey Miryanov <[email protected]>
Co-authored-by: Hugo van Kemenade <[email protected]>
2026-04-30 12:19:13 -07:00
Pablo Galindo SalgadoandGitHub 21fb9dc71d gh-146527: Heap-allocate gc_stats to avoid bloating PyInterpreterState (#148057)
The gc_stats struct contains ring buffers of gc_generation_stats
entries (11 young + 3×2 old on default builds). Embedding it inline
in _gc_runtime_state, which is itself inline in PyInterpreterState,
pushed fields like _gil.locked and threads.head to offsets beyond
what out-of-process profilers and debuggers can reasonably read in
a single buffer (e.g. offset 9384 for _gil.locked vs an 8 KiB read
buffer).

Heap-allocate generation_stats via PyMem_RawCalloc in _PyGC_Init and
free it in _PyGC_Fini. This shrinks PyInterpreterState by ~1.6 KiB
and keeps the GIL, thread-list, and other frequently-inspected fields
at stable, low offsets.
2026-04-04 18:42:30 +01:00
Sergey MiryanovandGitHub 5bf3a31bc2 GH-146527: Add more data to GC statistics and add it to PyDebugOffsets (#146532) 2026-03-28 18:52:10 +00:00
Peter BiermaandGitHub 19e64afddf gh-141070: Rename PyUnstable_Object_Dump to PyObject_Dump (GH-142848) 2026-01-16 09:19:43 -05:00
Brandt BucherandGitHub 227b9d326e GH-140638: Add a GC "candidates" stat (GH-141814) 2025-11-22 21:59:14 +00:00
Brandt BucherandGitHub 598d4c64de GH-140638: Add a GC "duration" stat (GH-141720) 2025-11-19 08:51:39 -08:00
600f3feb23 gh-141070: Add PyUnstable_Object_Dump() function (#141072)
* Promote _PyObject_Dump() as a public function.
* Keep _PyObject_Dump() alias to PyUnstable_Object_Dump()
  for backward compatibility.
* Replace _PyObject_Dump() with PyUnstable_Object_Dump().

Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
2025-11-18 16:13:13 +00:00
336366fd7c GH-140643: Add <native> and <GC> frames to the sampling profiler (#141108)
- Introduce a new field in the GC state to store the frame that initiated garbage collection.
- Update RemoteUnwinder to include options for including "<native>" and "<GC>" frames in the stack trace.
- Modify the sampling profiler to accept parameters for controlling the inclusion of native and GC frames.
- Enhance the stack collector to properly format and append these frames during profiling.
- Add tests to verify the correct behavior of the profiler with respect to native and GC frames, including options to exclude them.

Co-authored-by: Pablo Galindo Salgado <[email protected]>
2025-11-17 13:39:00 +00:00
Neil SchemenauerandGitHub c98c5b3449 gh-131253: free-threaded build support for pystats (gh-137189)
Allow the --enable-pystats build option to be used with free-threading.  The
stats are now stored on a per-interpreter basis, rather than process global.
For free-threaded builds, the stats structure is allocated per-thread and
then periodically merged into the per-interpreter stats structure (on thread
exit or when the reporting function is called). Most of the pystats related
code has be moved into the file Python/pystats.c.
2025-11-03 11:36:37 -08:00
Mark ShannonandGitHub 0c01090ad9 GH-139951: Fix major GC performance regression (GH-140262)
* Count number of actually tracked objects, instead of trackable objects. This ensures that untracking tuples has the desired effect of reducing GC overhead

* Do not track most untrackable tuples during creation. This prevents large numbers of small tuples causing execessive GCs.
2025-10-21 15:22:15 +01:00
Pål Grønås DrangeandGitHub b2f9fb9db2 gh-140358: Bring back elapsed time and unreachable count to gc debug output (#140359) 2025-10-21 00:54:44 +01:00
Sergey MiryanovandGitHub 654b8d9364 GH-137562: Fix github-issue number for deallocated objects in cache bug (GH-137614) 2025-08-12 10:32:53 -07:00
Sergey MiryanovandGitHub c744b57fd6 gh-137562: Remove reference for GC_REACHABLE in comment (GH-137563)
GC_REACHABLE is obsolete
2025-08-09 21:32:45 +09:00
Neil SchemenauerandGitHub 350c58ba4e GH-135552: Make the GC clear weakrefs later (GH-136189)
Fix a bug caused by the garbage collector clearing weakrefs too early.  The
weakrefs in the ``tp_subclasses`` dictionary are needed in order to correctly
invalidate type caches (for example, by calling ``PyType_Modified()``).
Clearing weakrefs before calling finalizers causes the caches to not be
correctly invalidated.  That can cause crashes since the caches can refer to
invalid objects.  Defer the clearing of weakrefs without callbacks until after
finalizers are executed.
2025-08-07 16:32:17 -07:00
Abhinav UpadhyayandGitHub e3ad9003c5 GH-119085: Move comment in Python/gc.c to correct place.
In GH-116206, the comment about moving reachable objects to next generation
got moved from its original place to a place where there is no code below
it. Put the comment back to where the actual movement of reachable objects
happens.
2025-08-07 20:31:11 +00:00
Sergey MiryanovandGitHub d7db0ee7ee gh-137084: remove multiple calls to get_gc_state in gc.c (#137085) 2025-07-25 18:13:54 +05:30
Sergey MiryanovandGitHub e93c30d466 gh-137054: remove obsolete counting of objects in young generation under Py_STATS builds (#137055) 2025-07-24 17:00:27 +05:30
Sergey MiryanovandGitHub c560df9658 gh-136517: Print uncollectable objects if DEBUG_UNCOLLECTABLE mode was set (#136518) 2025-07-10 22:13:23 +01:00
Neil SchemenauerandGitHub b6b99bf7f1 GH-91636: Clear weakrefs created by finalizers. (GH-136401)
Weakrefs to unreachable garbage that are created during running of
finalizers need to be cleared.  This avoids exposing objects that
have `tp_clear` called on them to Python-level code.
2025-07-08 12:19:57 -07:00
Kirill PodoprigoraandGitHub a525ba514a Python/gc.c: Refer to InternalDocs instead of devguide. (#136243) 2025-07-03 12:14:14 +00:00
Mark ShannonandGitHub f554237b8e GH-133261: Make sure that the GC doesn't untrack objects in trashcan (GH-133431) 2025-05-05 13:44:50 +01:00
Mark ShannonandGitHub 44e4c479fb GH-124715: Move trashcan mechanism into Py_Dealloc (GH-132280) 2025-04-30 11:37:53 +01:00
Mark ShannonandGitHub ccf1b0b1c1 GH-132508: Use tagged integers on the evaluation stack for the last instruction offset (GH-132545) 2025-04-29 18:00:35 +01:00
mpageandGitHub 053c285f6b gh-130704: Strength reduce LOAD_FAST{_LOAD_FAST} (#130708)
Optimize `LOAD_FAST` opcodes into faster versions that load borrowed references onto the operand stack when we can prove that the lifetime of the local outlives the lifetime of the temporary that is loaded onto the stack.
2025-04-01 10:18:42 -07:00
Martin DeMelloandGitHub 9c1e85fd64 gh-131740: minor readability fix in PyUnstable_GC_VisitObjects (gh-131786)
Minor readability fix in PyUnstable_GC_VisitObjects

Replaces `if (visit_generation())` with `if (visit_generation() < 0)`,
since we are checking for the failure case, and it's confusing to have
that be implicitly `true`.

Also fixes a misspelt variable name.
2025-03-29 08:58:17 +09:00
Donghee NaandGitHub 7bb41aef4b gh-131740: Update PyUnstable_GC_VisitObjects to traverse perm gen (gh-131744) 2025-03-26 09:45:29 +09:00
Victor StinnerandGitHub b69da006a4 gh-131238: Remove includes from pycore_interp.h (#131495)
Remove also now unused includes in C files.
2025-03-20 11:35:23 +00:00
Victor StinnerandGitHub 22706843e0 gh-131238: Remove many includes from pycore_interp.h (#131472) 2025-03-19 17:46:24 +00:00
Mark ShannonandGitHub 2bef8ea8ea GH-127705: Use _PyStackRefs in the default build. (GH-127875) 2025-03-10 14:06:56 +00:00
Sam GrossandGitHub 0559339ccd gh-130019: Fix data race in _PyType_AllocNoTrack (gh-130058)
The reference count fields, such as `ob_tid` and `ob_ref_shared`, may be
accessed concurrently in the free threading build by a `_Py_TryXGetRef`
or similar operation. The PyObject header fields will be initialized by
`_PyObject_Init`, so only call `memset()` to zero-initialize the remainder
of the allocation.
2025-02-13 11:50:45 -05:00
Mark ShannonandGitHub 72f56654d0 GH-128682: Account for escapes in DECREF_INPUTS (GH-129953)
* Handle escapes in DECREF_INPUTS

* Mark a few more functions as escaping

* Replace DECREF_INPUTS with PyStackRef_CLOSE where possible
2025-02-12 17:44:59 +00:00
Victor StinnerandGitHub 3447f4a56a gh-129354: Use PyErr_FormatUnraisable() function (#129514)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
2025-01-31 14:20:35 +01:00
Victor StinnerandGitHub 95504f429e gh-129354: Fix grammar in PyErr_FormatUnraisable() (#129475)
Replace "on verb+ing" with "while verb+ing".
2025-01-31 09:45:35 +01:00
Mark ShannonandGitHub f5b6356a11 GH-128563: Add new frame owner type for interpreter entry frames (GH-129078)
Add new frame owner type for interpreter entry frames
2025-01-21 10:15:02 +00:00
Petr ViktorinandGitHub 690fe077f6 gh-126491: Revert "GH-126491: Lower heap size limit with faster marking (GH-127519)" (GH-127770)
Revert "GH-126491: Lower heap size limit with faster marking (GH-127519)"

This reverts commit 023b7d2141, which introduced
a refleak.
2024-12-10 11:53:56 +01:00
Mark ShannonandGitHub 023b7d2141 GH-126491: Lower heap size limit with faster marking (GH-127519)
* Faster marking of reachable objects

* Changes calculation of work to do and work done.

* Merges transitive closure calculations
2024-12-06 10:46:59 +00:00
Mark ShannonandGitHub a8dd821d5b GH-126491: GC: Mark objects reachable from roots before doing cycle collection (GH-127110)
* Mark almost all reachable objects before doing collection phase

* Add stats for objects marked

* Visit new frames before each increment

* Update docs

* Clearer calculation of work to do.
2024-12-02 10:12:17 +00:00
Mark ShannonandGitHub aea0c586d1 GH-127010: Don't lazily track and untrack dicts (GH-127027) 2024-11-20 16:41:20 +00:00
Mark ShannonandGitHub a99dd23c1f GH-124567: Replace quadratic assert with linear one (GH-127009) 2024-11-19 13:38:59 +00:00
Hugo van KemenadeandGitHub 899fdb213d Revert "GH-126491: GC: Mark objects reachable from roots before doing cycle collection (GH-126502)" (#126983) 2024-11-19 11:25:09 +02:00
Mark ShannonandGitHub b0fcc2c47a GH-126491: GC: Mark objects reachable from roots before doing cycle collection (GH-126502)
* Mark almost all reachable objects before doing collection phase

* Add stats for objects marked

* Visit new frames before each increment

* Remove lazy dict tracking

* Update docs

* Clearer calculation of work to do.
2024-11-18 14:31:26 +00:00
Mark ShannonandGitHub c0f045f7fd GH-124567: Reduce overhead of debug build for GC. Should help CI performance (GH-126777) 2024-11-15 08:59:01 +00:00
Neil SchemenauerandGitHub 6f9525dd3f gh-116510: Fix crash during sub-interpreter shutdown (gh-124645)
Fix a bug that can cause a crash when sub-interpreters use "basic"
single-phase extension modules.  Shared objects could refer to PyGC_Head
nodes that had been freed as part of interpreter shutdown.
2024-09-26 19:33:07 -07:00
Sam GrossandGitHub b2afe2aae4 gh-123923: Defer refcounting for f_executable in _PyInterpreterFrame (#123924)
Use a `_PyStackRef` and defer the reference to `f_executable` when
possible. This avoids some reference count contention in the common case
of executing the same code object from multiple threads concurrently in
the free-threaded build.
2024-09-12 12:37:06 -04:00
Mark ShannonandGitHub f49a91648a GH-117759: Document incremental GC (GH-123266)
* Update what's new

* Update gc module docs and fix inconsistency in gc.get_objects
2024-08-27 15:23:39 +01:00
Mark ShannonandGitHub 7cd3aa42f0 GH-122298: Restore printing of GC stats (GH-123261) 2024-08-23 16:39:08 +01:00