-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
GH-117759: Update GC docs for incremental collection #126695
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
Merged
markshannon
merged 5 commits into
python:main
from
faster-cpython:update-gc-internal-docs
Nov 14, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -108,7 +108,7 @@ As is explained later in the | |||||||||
| [Optimization: reusing fields to save memory](#optimization-reusing-fields-to-save-memory) | ||||||||||
| section, these two extra fields are normally used to keep doubly linked lists of all the | ||||||||||
| objects tracked by the garbage collector (these lists are the GC generations, more on | ||||||||||
| that in the [Optimization: generations](#Optimization-generations) section), but | ||||||||||
| that in the [Optimization: incremental collection](#Optimization-incremental-collection) section), but | ||||||||||
| they are also reused to fulfill other purposes when the full doubly linked list | ||||||||||
| structure is not needed as a memory optimization. | ||||||||||
|
|
||||||||||
|
|
@@ -351,38 +351,91 @@ follows these steps in order: | |||||||||
| the reference counts fall to 0, triggering the destruction of all unreachable | ||||||||||
| objects. | ||||||||||
|
|
||||||||||
| Optimization: generations | ||||||||||
| ========================= | ||||||||||
| Optimization: incremental collection | ||||||||||
| ==================================== | ||||||||||
|
|
||||||||||
| In order to limit the time each garbage collection takes, the GC | ||||||||||
| implementation for the default build uses a popular optimization: | ||||||||||
| generations. The main idea behind this concept is the assumption that most | ||||||||||
| objects have a very short lifespan and can thus be collected soon after their | ||||||||||
| creation. This has proven to be very close to the reality of many Python | ||||||||||
| In order to bound the length of each garbage collection pause, the GC implementation | ||||||||||
| for the default build uses incremental collection with two generations. | ||||||||||
|
|
||||||||||
| Generational garbage collection takes advantage of what is known as the weak | ||||||||||
| generational hypothesis: Most objects die young. | ||||||||||
| This has proven to be very close to the reality of many Python | ||||||||||
| programs as many temporary objects are created and destroyed very quickly. | ||||||||||
|
|
||||||||||
| To take advantage of this fact, all container objects are segregated into | ||||||||||
| three spaces/generations. Every new | ||||||||||
| object starts in the first generation (generation 0). The previous algorithm is | ||||||||||
| executed only over the objects of a particular generation and if an object | ||||||||||
| survives a collection of its generation it will be moved to the next one | ||||||||||
| (generation 1), where it will be surveyed for collection less often. If | ||||||||||
| the same object survives another GC round in this new generation (generation 1) | ||||||||||
| it will be moved to the last generation (generation 2) where it will be | ||||||||||
| surveyed the least often. | ||||||||||
|
|
||||||||||
| The GC implementation for the free-threaded build does not use multiple | ||||||||||
| generations. Every collection operates on the entire heap. | ||||||||||
| two generations: young and old. Every new object starts in the young generation. | ||||||||||
| Each garbage collection scans the entire young generation and part of the old generation. | ||||||||||
|
|
||||||||||
| The time taken to scan the young generation can be controlled by controlling the | ||||||||||
| size of the young, but the size of the old generation cannot be controlled. | ||||||||||
|
||||||||||
| The time taken to scan the young generation can be controlled by controlling the | |
| size of the young, but the size of the old generation cannot be controlled. | |
| The time taken to scan the young generation can be controlled by controlling its | |
| size, but the size of the old generation cannot be controlled. |
iritkatriel marked this conversation as resolved.
Show resolved
Hide resolved
iritkatriel marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
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.
what does "by a single collection" mean here?
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.