Title: Add a private API to defer automatic garbage collection · Issue #153946 · python/cpython · GitHub
Open Graph Title: Add a private API to defer automatic garbage collection · Issue #153946 · python/cpython
X Title: Add a private API to defer automatic garbage collection · Issue #153946 · python/cpython
Description: I would like to propose a private API that allows CPython internals to defer automatic garbage collection on the current thread without changing the interpreter-visible GC state. Some CPython internals build large object graphs in a burs...
Open Graph Description: I would like to propose a private API that allows CPython internals to defer automatic garbage collection on the current thread without changing the interpreter-visible GC state. Some CPython inter...
X Description: I would like to propose a private API that allows CPython internals to defer automatic garbage collection on the current thread without changing the interpreter-visible GC state. Some CPython inter...
Opengraph URL: https://github.com/python/cpython/issues/153946
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add a private API to defer automatic garbage collection","articleBody":"I would like to propose a private API that allows CPython internals to defer automatic garbage collection on the current thread without changing the interpreter-visible GC state.\n\nSome CPython internals build large object graphs in a burst, where a collection triggered halfway through cannot reclaim anything from that operation and only traverses a half-built graph. Using `PyGC_Disable()` for this changes interpreter-wide state. Saving and restoring that state is racy in the free-threaded build, and it can also be observed or changed by user code in the GIL build.\n\nWhat we want is something approximately like:\n\n```c\n_PyGC_DeferAutomaticCollection(tstate);\n/* Build a large object graph. */\n_PyGC_ResumeAutomaticCollection(tstate);\n```\n\nThis would be a private CPython API. It should be per-thread and nestable, keep allocation counters running, leave `gc.isenabled()` unchanged, and defer only automatic collection. Leaving the outermost scope would schedule a collection if one became due.\n\nI think there are many possible cases in the stdlib and core that can benefit from this, and we currently do not have a safe tool for them. This can be implemented with a small nesting counter in `_PyThreadStateImpl`, checked by the allocation paths and `_Py_RunGC()`. This would allow these optimizations without changing user-visible GC state or introducing cross-thread races.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154188\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/pablogsal","@type":"Person","name":"pablogsal"},"datePublished":"2026-07-18T12:58:53Z","url":"https://github.com/python/cpython/issues/153946"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2cf973cf-a3a3-aac0-d150-4b8a25217adb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9652:19AA62:3CE12A6:4D385D1:6B020875 |
| html-safe-nonce | 721e1ddc34f140f107cb56fadb92a562fb933199456ea07942ca53f658eed196 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NjUyOjE5QUE2MjozQ0UxMkE2OjREMzg1RDE6NkIwMjA4NzUiLCJ2aXNpdG9yX2lkIjoiMTExMzc4MzY3NzA0Njg4NDQ2OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 37c4aaa5e02ae48c5772b22d5b7394e782978c1736a47419d71a3e76ebf5b458 |
| hovercard-subject-tag | issue:4918351231 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/153946/issue_layout |
| twitter:image | https://opengraph.githubassets.com/513658bd9a28d7a4e89beeae75f16e4ee613b9585f06c6271a838d1138ec065e/python/cpython/issues/153946 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/513658bd9a28d7a4e89beeae75f16e4ee613b9585f06c6271a838d1138ec065e/python/cpython/issues/153946 |
| og:image:alt | I would like to propose a private API that allows CPython internals to defer automatic garbage collection on the current thread without changing the interpreter-visible GC state. Some CPython inter... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pablogsal |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width