Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GC: Implementation does not match behavior documented on gc.set_threshold #122862

Open
ojw28 opened this issue Aug 9, 2024 · 1 comment
Open
Labels
docs Documentation in the Doc dir

Comments

@ojw28
Copy link

ojw28 commented Aug 9, 2024

Bug report

Bug description:

The documentation of gc.set_threshold states:

In order to decide when to run, the collector keeps track of the number object allocations and deallocations since the last collection. When the number of allocations minus the number of deallocations exceeds threshold0, collection starts

This doesn't seem to be accurate. Looking at the implementation, it appears that young.count is being used to track number of allocations minus the number of deallocations. However it doesn't actually track this due to the if (gcstate->young.count > 0) check here: https://github.com/python/cpython/blob/3.13/Python/gc.c#L2119.

As a concrete example, suppose threshold0=500. Suppose the GC runs and young.count is reset to 0, then 500 deallocations happen (by object ref counts dropping to 0), then 500 allocations happen. According to the documentation the GC would not run in this case, because the difference between allocations and deallocations is 0. However the actual tracked number in young.count will be 500, and GC will trigger.

I'm unsure which is the expected behavior; the documented one or the implemented one, but it does appear that the two do not currently agree.

CPython versions tested on:

3.13

Operating systems tested on:

macOS

@ojw28 ojw28 added the type-bug An unexpected behavior, bug, or error label Aug 9, 2024
@aisk
Copy link
Contributor

aisk commented Aug 11, 2024

Usually the document is just the description of current implementation, so I guess the implementation is the expected behavior. Thus I will change the label to doc.

@aisk aisk added docs Documentation in the Doc dir and removed type-bug An unexpected behavior, bug, or error labels Aug 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

2 participants