-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
gh-103059: Clarify gc.freeze documentation #103058
Conversation
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the proposal and PR!
I definitely agree that the gc.freeze()
docs should be cleaned up and clarified, but I don't agree with adding a bunch of fork-specific text to the gc.disable()
docs. There are lots of possible reasons to disable GC; "I'm going to fork-without-exec later" is only one of them, and probably not the most common. And in that use case, gc.disable()
is still useless unless you also gc.freeze()
. So the only API that is specific to the fork-without-exec use case is gc.freeze()
, and the explanation of how to handle fork-without-exec belongs there.
Here's my full suggested rewrite for the gc.freeze()
documentation:
Freeze all objects tracked by the garbage collector: move them to a permanent generation and ignore them in all future collections.
If a process will
fork()
withoutexec()
, avoiding unnecessary copy-on-write in child processes will maximize memory sharing and reduce overall memory usage. This requires both avoiding creation of freed "holes" in memory pages in the parent process, and ensuring that GC collections in child processes won't touch the memory of long-lived objects originating in the parent process. To accomplish both, callgc.disable()
early in the parent process,gc.freeze()
right beforefork()
, andgc.enable()
early in child processes.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
The current advice seems to mean you should gc.disable() and gc.freeze() right before os.fork(), which doesn't do anything. Łukasz explained the advice in https://bugs.python.org/issue31558#msg302780. This moves the advice around gc.disable out of the gc.freeze docs.
I left in the bit about I have made the requested changes; please review again |
Thanks for making the requested changes! @carljm: please review the changes made to this pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks accurate and like a solid improvement to me. Thanks for the PR!
Process nit for future contributions to CPython: it's better to never force-push your PR. It can make life more difficult for reviewers (can't see changes since previously-reviewed version), and there's no benefit, since we always squash on merge anyway.
Misc/NEWS.d/next/Documentation/2023-03-27-18-19-35.gh-issue-103059.55_kOa.rst
Outdated
Show resolved
Hide resolved
This comment was marked as outdated.
This comment was marked as outdated.
GH-103416 is a backport of this pull request to the 3.11 branch. |
(cherry picked from commit 8b1b171) Co-authored-by: raylu <lurayl@gmail.com>
The current advice seems to mean you should gc.disable() and gc.freeze() right before os.fork(), which doesn't do anything.
Łukasz explained the advice in https://bugs.python.org/issue31558#msg302780.
This moves the advice around gc.disable out of the gc.freeze docs.