GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-146527: Fix memory leak in _PyGC_Fini() by vstinner · Pull Request #150969 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Jun 5, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Free generation_stats allocated by _PyGC_Init().

Fix Python/gc.c: Python/gc_free_threading.c was already fixed.

  • Issue: Add a module for monitoring GC statistics #146527

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
pythongh-146527: Fix memory leak in _PyGC_Fini()
18b231f
Free generation_stats allocated by _PyGC_Init().

Fix Python/gc.c: Python/gc_free_threading.c was already fixed.
@vstinner vstinner added the needs backport to 3.15 bugs and security fixes label Jun 5, 2026
@bedevere-app bedevere-app Bot added the awaiting core review label Jun 5, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Jun 5, 2026
Add a module for monitoring GC statistics #146527
Closed
@vstinner

vstinner commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

For example, the leak can be seen with Valgrind.

$ PYTHONMALLOC=malloc valgrind --leak-check=full --show-leak-kinds=all --num-callers=20 ./python -c pass
(...)
==3201846== HEAP SUMMARY:
==3201846==     in use at exit: 1,112 bytes in 1 blocks
==3201846==   total heap usage: 28,765 allocs, 28,764 frees, 6,620,982 bytes allocated
==3201846== 
==3201846== 1,112 bytes in 1 blocks are still reachable in loss record 1 of 1
==3201846==    at 0x484862B: calloc (vg_replace_malloc.c:1616)
==3201846==    by 0x4FE115: _PyMem_RawCalloc (obmalloc.c:80)
==3201846==    by 0x511FC1: PyMem_RawCalloc (obmalloc.c:1222)
==3201846==    by 0x62A744: _PyGC_Init (gc.c:137)
==3201846==    by 0x65DB65: pycore_interp_init (pylifecycle.c:975)
==3201846==    by 0x65DD71: pyinit_config (pylifecycle.c:1040)
==3201846==    by 0x666D3A: pyinit_core (pylifecycle.c:1203)
==3201846==    by 0x666E13: Py_InitializeFromConfig (pylifecycle.c:1631)
==3201846==    by 0x69888E: pymain_init (main.c:69)
==3201846==    by 0x698953: pymain_main (main.c:817)
==3201846==    by 0x6989E4: Py_BytesMain (main.c:850)
==3201846==    by 0x401CEE: main (python.c:15)
==3201846== 
==3201846== LEAK SUMMARY:
==3201846==    definitely lost: 0 bytes in 0 blocks
==3201846==    indirectly lost: 0 bytes in 0 blocks
==3201846==      possibly lost: 0 bytes in 0 blocks
==3201846==    still reachable: 1,112 bytes in 1 blocks
==3201846==         suppressed: 0 bytes in 0 blocks
(...)

I found the leak when building Python with ASAN.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner added the skip news label Jun 5, 2026
@vstinner vstinner mentioned this pull request Jun 5, 2026
gh-146527: Heap-allocate gc_stats to avoid bloating PyInterpreterState #148057
Merged
@vstinner
Oops, use PyMem_RawFree() instead of PyMem_Free()
5b8c03f
@vstinner
vstinner enabled auto-merge (squash) June 5, 2026 13:13
pablogsal
pablogsal approved these changes Jun 5, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Jun 5, 2026
@pablogsal

pablogsal commented Jun 5, 2026

Copy link
Copy Markdown
Member

Thanks victor!

vstinner reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner merged commit 0036565 into python:main Jun 5, 2026
56 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner deleted the free_gc branch June 5, 2026 13:43
@miss-islington-app

miss-islington-app Bot commented Jun 5, 2026

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Jun 5, 2026
@bedevere-app

bedevere-app Bot commented Jun 5, 2026

Copy link
Copy Markdown

GH-150970 is a backport of this pull request to the 3.15 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Jun 5, 2026
vstinner added a commit that referenced this pull request Jun 5, 2026
@miss-islington @vstinner
[3.15] gh-146527: Fix memory leak in _PyGC_Fini() (GH-150969) (#150970)
06ffcde
gh-146527: Fix memory leak in _PyGC_Fini() (GH-150969)

Free generation_stats allocated by _PyGC_Init().

Fix Python/gc.c: Python/gc_free_threading.c was already fixed.
(cherry picked from commit 0036565)

Co-authored-by: Victor Stinner <[email protected]>
@sergey-miryanov

sergey-miryanov commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Thanks!

vstinner reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

philthompson10 pushed a commit to philthompson10/cpython that referenced this pull request Jun 17, 2026
@vstinner @philthompson10
pythongh-146527: Fix memory leak in _PyGC_Fini() (python#150969)
1765da4
Free generation_stats allocated by _PyGC_Init().

Fix Python/gc.c: Python/gc_free_threading.c was already fixed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@pablogsal pablogsal pablogsal approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@vstinner @pablogsal @sergey-miryanov
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.