@@ -17,26 +17,26 @@ value returned by this function is always 1 more as the function also has a refe
1717to the object when called):
1818
1919``` 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
2929```
3030
3131The main problem with the reference counting scheme is that it does not handle reference
3232cycles. For instance, consider this code:
3333
3434``` 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
4040```
4141
4242In 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
199199unreachable:
200200
201201``` 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
222222```
223223
224224The 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
439439function:
440440
441441``` pycon
442- >>> import gc
443- >>> gc.get_threshold()
444- (700, 10, 10)
442+ >>> import gc
443+ >>> gc.get_threshold()
444+ (700, 10, 10)
445445```
446446
447447The content of these generations can be examined using the
448448` gc.get_objects(generation=NUM) ` function and collections can be triggered
449449specifically in a generation by calling ` gc.collect(generation=NUM) ` .
450450
451451``` 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>, ...]
484478```
485479
486480
@@ -563,18 +557,18 @@ the current tracking status of the object. Subsequent garbage collections may ch
563557tracking status of the object.
564558
565559``` 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
578572```
579573
580574Differences between GC implementations
0 commit comments