@@ -17,26 +17,26 @@ value returned by this function is always 1 more as the function also has a refe
17
17
to the object when called):
18
18
19
19
``` pycon
20
- >>> x = object()
21
- >>> sys.getrefcount(x)
22
- 2
23
- >>> y = x
24
- >>> sys.getrefcount(x)
25
- 3
26
- >>> del y
27
- >>> sys.getrefcount(x)
28
- 2
20
+ >>> x = object ()
21
+ >>> sys.getrefcount(x)
22
+ 2
23
+ >>> y = x
24
+ >>> sys.getrefcount(x)
25
+ 3
26
+ >>> del y
27
+ >>> sys.getrefcount(x)
28
+ 2
29
29
```
30
30
31
31
The main problem with the reference counting scheme is that it does not handle reference
32
32
cycles. For instance, consider this code:
33
33
34
34
``` pycon
35
- >>> container = []
36
- >>> container.append(container)
37
- >>> sys.getrefcount(container)
38
- 3
39
- >>> del container
35
+ >>> container = []
36
+ >>> container.append(container)
37
+ >>> sys.getrefcount(container)
38
+ 3
39
+ >>> del container
40
40
```
41
41
42
42
In this example, ` container ` holds a reference to itself, so even when we remove
@@ -199,26 +199,26 @@ variable `A`, and one self-referencing object which is completely
199
199
unreachable:
200
200
201
201
``` pycon
202
- >>> import gc
203
-
204
- >>> class Link:
205
- ... def __init__(self, next_link=None):
206
- ... self.next_link = next_link
207
-
208
- >>> link_3 = Link()
209
- >>> link_2 = Link(link_3)
210
- >>> link_1 = Link(link_2)
211
- >>> link_3.next_link = link_1
212
- >>> A = link_1
213
- >>> del link_1, link_2, link_3
214
-
215
- >>> link_4 = Link()
216
- >>> link_4.next_link = link_4
217
- >>> del link_4
218
-
219
- # Collect the unreachable Link object (and its .__dict__ dict).
220
- >>> gc.collect()
221
- 2
202
+ >>> import gc
203
+ >>>
204
+ >>> class Link :
205
+ ... def __init__ (self , next_link = None ):
206
+ ... self .next_link = next_link
207
+ ...
208
+ >>> link_3 = Link()
209
+ >>> link_2 = Link(link_3)
210
+ >>> link_1 = Link(link_2)
211
+ >>> link_3.next_link = link_1
212
+ >>> A = link_1
213
+ >>> del link_1, link_2, link_3
214
+ >>>
215
+ >>> link_4 = Link()
216
+ >>> link_4.next_link = link_4
217
+ >>> del link_4
218
+ >>>
219
+ >>> # Collect the unreachable Link object (and its .__dict__ dict).
220
+ >>> gc.collect()
221
+ 2
222
222
```
223
223
224
224
The GC starts with a set of candidate objects it wants to scan. In the
@@ -439,48 +439,42 @@ These thresholds can be examined using the
439
439
function:
440
440
441
441
``` pycon
442
- >>> import gc
443
- >>> gc.get_threshold()
444
- (700, 10, 10)
442
+ >>> import gc
443
+ >>> gc.get_threshold()
444
+ (700, 10, 10)
445
445
```
446
446
447
447
The content of these generations can be examined using the
448
448
` gc.get_objects(generation=NUM) ` function and collections can be triggered
449
449
specifically in a generation by calling ` gc.collect(generation=NUM) ` .
450
450
451
451
``` pycon
452
- >>> import gc
453
- >>> class MyObj:
454
- ... pass
455
- ...
456
-
457
- # Move everything to the old generation so it's easier to inspect
458
- # the young generation.
459
-
460
- >>> gc.collect()
461
- 0
462
-
463
- # Create a reference cycle.
464
-
465
- >>> x = MyObj()
466
- >>> x.self = x
467
-
468
- # Initially the object is in the young generation.
469
-
470
- >>> gc.get_objects(generation=0)
471
- [..., <__main__.MyObj object at 0x7fbcc12a3400>, ...]
472
-
473
- # After a collection of the youngest generation the object
474
- # moves to the old generation.
475
-
476
- >>> gc.collect(generation=0)
477
- 0
478
- >>> gc.get_objects(generation=0)
479
- []
480
- >>> gc.get_objects(generation=1)
481
- []
482
- >>> gc.get_objects(generation=2)
483
- [..., <__main__.MyObj object at 0x7fbcc12a3400>, ...]
452
+ >>> import gc
453
+ >>> class MyObj :
454
+ ... pass
455
+ ...
456
+ >>> # Move everything to the old generation so it's easier to inspect
457
+ >>> # the young generation.
458
+ >>> gc.collect()
459
+ 0
460
+ >>> # Create a reference cycle.
461
+ >>> x = MyObj()
462
+ >>> x.self = x
463
+ >>>
464
+ >>> # Initially the object is in the young generation.
465
+ >>> gc.get_objects(generation = 0 )
466
+ [..., <__main__.MyObj object at 0x7fbcc12a3400>, ...]
467
+ >>>
468
+ >>> # After a collection of the youngest generation the object
469
+ >>> # moves to the old generation.
470
+ >>> gc.collect(generation = 0 )
471
+ 0
472
+ >>> gc.get_objects(generation = 0 )
473
+ []
474
+ >>> gc.get_objects(generation = 1 )
475
+ []
476
+ >>> gc.get_objects(generation = 2 )
477
+ [..., <__main__.MyObj object at 0x7fbcc12a3400>, ...]
484
478
```
485
479
486
480
@@ -563,18 +557,18 @@ the current tracking status of the object. Subsequent garbage collections may ch
563
557
tracking status of the object.
564
558
565
559
``` pycon
566
- >>> gc.is_tracked(0)
567
- False
568
- >>> gc.is_tracked("a")
569
- False
570
- >>> gc.is_tracked([])
571
- True
572
- >>> gc.is_tracked(())
573
- False
574
- >>> gc.is_tracked({})
575
- True
576
- >>> gc.is_tracked({"a": 1})
577
- True
560
+ >>> gc.is_tracked(0 )
561
+ False
562
+ >>> gc.is_tracked(" a" )
563
+ False
564
+ >>> gc.is_tracked([])
565
+ True
566
+ >>> gc.is_tracked(())
567
+ False
568
+ >>> gc.is_tracked({})
569
+ True
570
+ >>> gc.is_tracked({" a" : 1 })
571
+ True
578
572
```
579
573
580
574
Differences between GC implementations
0 commit comments