Title: Free-threaded build never returns freed memory to the OS on macOS, even with gc.collect() and MIMALLOC_PURGE_DELAY=0 · Issue #158972 · python/cpython · GitHub
Open Graph Title: Free-threaded build never returns freed memory to the OS on macOS, even with gc.collect() and MIMALLOC_PURGE_DELAY=0 · Issue #158972 · python/cpython
X Title: Free-threaded build never returns freed memory to the OS on macOS, even with gc.collect() and MIMALLOC_PURGE_DELAY=0 · Issue #158972 · python/cpython
Description: Bug report On the free-threaded build, memory freed after a large transient allocation is never returned to the OS. RSS stays at the peak permanently. The free-threading HOWTO says gc.collect() releases memory held by QSBR, and that MIMA...
Open Graph Description: Bug report On the free-threaded build, memory freed after a large transient allocation is never returned to the OS. RSS stays at the peak permanently. The free-threading HOWTO says gc.collect() rel...
X Description: Bug report On the free-threaded build, memory freed after a large transient allocation is never returned to the OS. RSS stays at the peak permanently. The free-threading HOWTO says gc.collect() rel...
Opengraph URL: https://github.com/python/cpython/issues/158972
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Free-threaded build never returns freed memory to the OS on macOS, even with gc.collect() and MIMALLOC_PURGE_DELAY=0","articleBody":"### Bug report\n\nOn the free-threaded build, memory freed after a large transient allocation is never returned to the OS. RSS stays at the peak permanently. The free-threading HOWTO says gc.collect() releases memory held by QSBR, and that MIMALLOC_PURGE_DELAY=0 reduces mimalloc's delay before returning freed memory to the OS. On macOS neither changes anything and RSS does not drop at all.\n\nThe default (GIL) build of the same version returns almost everything.\n\nReproduction script:\n```python\nimport gc\nimport os\nimport subprocess\nimport sys\nimport time\n\n\ndef rss_mib():\n out = subprocess.run([\"ps\", \"-o\", \"rss=\", \"-p\", str(os.getpid())], capture_output=True, text=True)\n return int(out.stdout) // 1024\n\n\nfree_threaded = not sys._is_gil_enabled()\nprint(sys.version.split()[0], \"free-threaded\" if free_threaded else \"default (GIL)\")\nprint(f\"start {rss_mib():6d} MiB\")\n\njunk = [[i] * 30 for i in range(4_000_000)]\nprint(f\"after allocating {rss_mib():6d} MiB\")\n\ndel junk\ngc.collect()\ngc.collect()\nprint(f\"after del + gc.collect() {rss_mib():6d} MiB\")\n\ndeadline = time.monotonic() + 5\nwhile time.monotonic() \u003c deadline:\n [str(i) for i in range(20_000)]\ngc.collect()\nprint(f\"after 5s more activity {rss_mib():6d} MiB\")\n```\n\nOutput from macOS 26.7.1, arm64:\n```\n$ PYTHON_GIL=0 python3.14t repro.py\n3.14.8 free-threaded\nstart 21 MiB\nafter allocating 1466 MiB\nafter del + gc.collect() 1466 MiB\nafter 5s more activity 1466 MiB\n\n$ MIMALLOC_PURGE_DELAY=0 PYTHON_GIL=0 python3.14t repro.py\n3.14.8 free-threaded\nstart 21 MiB\nafter allocating 1466 MiB\nafter del + gc.collect() 1466 MiB\nafter 5s more activity 1466 MiB\n\n$ python3.14 repro.py\n3.14.8 default (GIL)\nstart 16 MiB\nafter allocating 1335 MiB\nafter del + gc.collect() 50 MiB\nafter 5s more activity 51 MiB\n```\n\nAs far as I can tell, its the same behaviour on 3.13.16t and 3.15.0rc2t (measured with vmmap though). on the 3.15 version, MIMALLOC_PURGE_DELAY=0 makes no difference as well. This whole issue could be related to https://github.com/microsoft/mimalloc/issues/1422 but im not entirely certain.\n\nI have tried a few mimalloc options as well with no luck. It also doesn't seem to matter if the allocation and free-ing happen on the main thread or a worker thread that exits or a worker thread that stays alive for a long period.\n\nThis seems to peak out depending on what you are doing on the threads rather than leaking forever in an unbounded way, if I do a repeated 0.5GBish allocate-then-free several times (on the same or a new one every time) then RSS stays approximately flat at whatever its first peak hit.\n\n`vmmap` is reporting the retained memory as belonging to IOAccelerator but I believe that is because the `mi_options_os_tag` defaults to 100 which maps to IOAccelerator on mac. It may be worth choosing a different value for this as it originally caused me to misdiagnose this as a GPU memory leak.\n\n### CPython versions tested on:\n\n3.14, 3.15, 3.13\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160150\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/dotdandotunderscore","@type":"Person","name":"dotdandotunderscore"},"datePublished":"2026-10-07T15:32:09Z","url":"https://github.com/python/cpython/issues/158972"}
| 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:26d1bafe-9698-3b3e-79ba-a633eed12d00 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A9F0:215B92:34D60FD:465E0EE:6B020194 |
| html-safe-nonce | 3eb0c5537965a22009100f643d676224f490fbad500d5abc36079eed1b93a540 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOUYwOjIxNUI5MjozNEQ2MEZEOjQ2NUUwRUU6NkIwMjAxOTQiLCJ2aXNpdG9yX2lkIjoiODY1ODQ4NTQyMzMyMzIyMTk3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1d9fb83c62bac5a7592ffeca427307899df222224df5452ab80f082d98f55cb0 |
| hovercard-subject-tag | issue:5747551102 |
| 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/158972/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ad29e2d90ccd17e728b36eb6e63e4a479a89748088733fce0ae839d649d7bf71/python/cpython/issues/158972 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ad29e2d90ccd17e728b36eb6e63e4a479a89748088733fce0ae839d649d7bf71/python/cpython/issues/158972 |
| og:image:alt | Bug report On the free-threaded build, memory freed after a large transient allocation is never returned to the OS. RSS stays at the peak permanently. The free-threading HOWTO says gc.collect() rel... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dotdandotunderscore |
| 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