Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions InternalDocs/garbage_collector.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ count field can be examined using the `sys.getrefcount()` function (notice that
value returned by this function is always 1 more as the function also has a reference
to the object when called):

```python
```pycon
>>> x = object()
>>> sys.getrefcount(x)
2
Expand All @@ -31,7 +31,7 @@ to the object when called):
The main problem with the reference counting scheme is that it does not handle reference
cycles. For instance, consider this code:

```python
```pycon
>>> container = []
>>> container.append(container)
>>> sys.getrefcount(container)
Expand Down Expand Up @@ -198,25 +198,25 @@ the case of a circular linked list which has one link referenced by a
variable `A`, and one self-referencing object which is completely
unreachable:

```python
```pycon
>>> import gc

>>>
>>> class Link:
... def __init__(self, next_link=None):
... self.next_link = next_link

...
>>> link_3 = Link()
>>> link_2 = Link(link_3)
>>> link_1 = Link(link_2)
>>> link_3.next_link = link_1
>>> A = link_1
>>> del link_1, link_2, link_3

>>>
>>> link_4 = Link()
>>> link_4.next_link = link_4
>>> del link_4

# Collect the unreachable Link object (and its .__dict__ dict).
>>>
>>> # Collect the unreachable Link object (and its .__dict__ dict).
>>> gc.collect()
2
```
Expand Down Expand Up @@ -438,7 +438,7 @@ These thresholds can be examined using the
[`gc.get_threshold()`](https://docs.python.org/3/library/gc.html#gc.get_threshold)
function:

```python
```pycon
>>> import gc
>>> gc.get_threshold()
(700, 10, 10)
Expand All @@ -448,31 +448,23 @@ The content of these generations can be examined using the
`gc.get_objects(generation=NUM)` function and collections can be triggered
specifically in a generation by calling `gc.collect(generation=NUM)`.

```python
```pycon
>>> import gc
>>> class MyObj:
... pass
...

# Move everything to the old generation so it's easier to inspect
# the young generation.

>>> # Move everything to the old generation so it's easier to inspect the young generation.
>>> gc.collect()
0

# Create a reference cycle.

>>> # Create a reference cycle.
>>> x = MyObj()
>>> x.self = x

# Initially the object is in the young generation.

>>>
>>> # Initially the object is in the young generation.
>>> gc.get_objects(generation=0)
[..., <__main__.MyObj object at 0x7fbcc12a3400>, ...]

# After a collection of the youngest generation the object
# moves to the old generation.

>>>
>>> # After a collection of the youngest generation the object moves to the old generation.
>>> gc.collect(generation=0)
0
>>> gc.get_objects(generation=0)
Expand Down Expand Up @@ -571,7 +563,7 @@ The garbage collector module provides the Python function `is_tracked(obj)`, whi
the current tracking status of the object. Subsequent garbage collections may change the
tracking status of the object.

```python
```pycon
>>> gc.is_tracked(0)
False
>>> gc.is_tracked("a")
Expand Down
Loading