Title: Free threading memory leak · Issue #145615 · python/cpython · GitHub
Open Graph Title: Free threading memory leak · Issue #145615 · python/cpython
X Title: Free threading memory leak · Issue #145615 · python/cpython
Description: This bug was originally found by a colleague. Memory usage (RSS) keeps increasing even though allocated blocks stays essentially constant. Self contained reproducer below: import gc import sys import os import concurrent.futures def get_...
Open Graph Description: This bug was originally found by a colleague. Memory usage (RSS) keeps increasing even though allocated blocks stays essentially constant. Self contained reproducer below: import gc import sys impo...
X Description: This bug was originally found by a colleague. Memory usage (RSS) keeps increasing even though allocated blocks stays essentially constant. Self contained reproducer below: import gc import sys impo...
Opengraph URL: https://github.com/python/cpython/issues/145615
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Free threading memory leak","articleBody":"This bug was originally found by a colleague. Memory usage (RSS) keeps increasing even though allocated blocks stays essentially constant. Self contained reproducer below:\n\n```python\nimport gc\nimport sys\nimport os\nimport concurrent.futures\n\ndef get_rss_mb():\n with open(\"/proc/self/statm\") as f:\n return int(f.read().split()[1]) * os.sysconf(\"SC_PAGE_SIZE\") / (1024 * 1024)\n\ndef work(_):\n return [bytearray(64) for _ in range(4000)]\n\n\nNUM_ITERATIONS = int(sys.argv[1]) if len(sys.argv) > 1 else 20\n\npool = concurrent.futures.ThreadPoolExecutor(max_workers=4)\nfor i in range(NUM_ITERATIONS):\n futures = [pool.submit(work, j) for j in range(100)]\n results = [f.result() for f in futures]\n del futures, results # free worker-allocated objects on main thread\n gc.collect()\n gc.collect()\n blocks = sys.getallocatedblocks()\n print(f\"iter {i:3d}: RSS={get_rss_mb():7.1f} MB {blocks=}\")\n\npool.shutdown()\nprint(f\"after shutdown: RSS={get_rss_mb():7.1f} MB\")\n```\n\nThe problem is that we end up having orphaned mimalloc pages in the full queue, even though those pages have available blocks or are completely empty. They're \"leaked\" until the thread exits.\n\n**NOTE**: The bugs are limited out our mimalloc modifications. They're not in upstream mimalloc.\n\nI think there are at least two underlying bugs here:\n\n1) `_PyMem_mi_page_maybe_free` leaves empty pages in the full page queue. This bug can happen when a page goes from \"full\" to \"all free\".\n2) `_mi_page_free_collect` calls `_PyMem_mi_page_clear_qsbr` if `page->local_free != NULL`. This can get erroneously triggered even the by traversing heaps. We only want to clear the page QSBR counter when it becomes empty again.\n\nAdditionally, `_PyMem_mi_page_maybe_free` can insert pages into the wrong mimalloc list. `_PyMem_mi_page_maybe_free` inserts the page into the *current thread's* list. However, during GC and other stop-the-world pauses, we may traverse the heaps of other threads. That traversal does some processing of free lists, which can end up calling `_PyMem_mi_page_maybe_free`. In that case, we should insert the page into the *owning thread's* list, **not** the current thread's list. Otherwise, we can end up with a data race later on.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-145626\n* gh-145691\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/colesbury","@type":"Person","name":"colesbury"},"datePublished":"2026-03-07T01:34:26Z","url":"https://github.com/python/cpython/issues/145615"}
| 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:8eeef6e7-7063-a80f-b1be-ee0c1e89f258 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C894:2270F4:34F49BE:463C20E:6B01FB5E |
| html-safe-nonce | b915681985c5919135d3bba1c026bbfa06bbe68710412bfa5a3866504049f292 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODk0OjIyNzBGNDozNEY0OUJFOjQ2M0MyMEU6NkIwMUZCNUUiLCJ2aXNpdG9yX2lkIjoiMTk1MDk0MDkzODM5ODQ2NDg2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2d98f53c5cf6f5887ce0df56597607870101f4b5223a04b797b9e8a539f001d2 |
| hovercard-subject-tag | issue:4036931195 |
| 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/145615/issue_layout |
| twitter:image | https://opengraph.githubassets.com/97c6f60d9710022da61cd037f9284372456a2f7d9bb3d0b86d2a53b0ea878bc3/python/cpython/issues/145615 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/97c6f60d9710022da61cd037f9284372456a2f7d9bb3d0b86d2a53b0ea878bc3/python/cpython/issues/145615 |
| og:image:alt | This bug was originally found by a colleague. Memory usage (RSS) keeps increasing even though allocated blocks stays essentially constant. Self contained reproducer below: import gc import sys impo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | colesbury |
| 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