5. Reference Counting
● Reference count is maintained for each
object on the heap.
● When an object is first created and a
reference to it is assigned to a variable, the
object's reference count is set to one.
6. Reference Counting
● When any other variable is assigned a
reference to that object, the object's count is
● When a reference to an object goes out of
scope or is assigned a new value, the
object's count is decremented.
7. a = 5000
a = 5000
a = 5000
a = 3000
8. a = 5000
a = 3000
b = 4000
9. Reference Counting
suitable for real-time environments where
the program can't be interrupted for very long.
reference counting does not detect cycles.
10. a = 
b = 
a = 
b = 
a = None
b = None
11. mark and sweep
1. Find the root objects of the system. These
are things like the global environment (like
the __main__ module in Python) and objects
on the stack.
2. Search from these objects and find all
objects reachable from them. This objects
are all "alive".
3. Free all other objects.
12. Two-Color Mark & Sweep
13. Two-Color Mark & Sweep
● the algorithm is non-incremental (atomic
14. Tri-Color Incremental Mark & Sweep
● Initially grey set is all the objects that are reachable from
root references but the objects referenced by grey
objects haven't been scanned yet.
● The white setis the set of objects that are candidates for
having their memory recycled.
● The black set is the set of objects that can cheaply be
proven to have no references to objects in the white set.
16. Tri-Color Incremental Mark & Sweep
● When there are no more objects in the grey
set, then all the objects remaining in the
white set have been demonstrated not to be
reachable, and the storage occupied by
them can be reclaimed.
17. Generational Collectors
1. Most objects created by most programs
have very short lives.
2. Most programs create some objects that
have very long lifetimes. A major source of
inefficiency in simple copying collectors is
that they spend much of their time copying
the same long-lived objects again and again.
18. External Memory fragment
● Free memory is separated into small blocks
and is interspersed by allocated memory.
● Although free storage is available, it is
unusable because it is divided into pieces
that are too small individually to satisfy the
demands of the application.
19. External Memory fragment
We can’t create a variable with four
20. Compacting and copying
● Move objects on the fly to reduce heap
22. stop and copy
● The heap is divided into two regions.
● Only one of the two regions is used at any time.
● Objects are allocated from one of the regions until all
the space in that region has been exhausted.
● Find out live objects and copy them to the other region.
● Memory will be allocated from the new heap region until
it too runs out of space
24. Python garbage collection
● Python use both of reference count and
“mark and sweep”.
● “mark and sweep” only work for containers
for solving reference cycles.
● Containers mean list, dict, instance, etc.
● python 的 mark and sweep和傳統方法不一
樣，因為 c extentsion 的存在，因此很難有共
同的 root object。
25. Python mark and sweep
1. For each container object, set gc_refs equal
to the object's reference count.
2. For each container object, find which
container objects it references and decrement
the referenced container's gc_refs field.
26. Python mark and sweep
3. All container objects that now have a gc_refs field
greater than one are referenced from outside the set of
container objects. We cannot free these objects so we
move them to a different set.
4. Any objects referenced from the objects moved also
cannot be freed. We move them and all the objects
reachable from them too.
27. Python mark and sweep
5. Objects left in our original set are referenced
only by objects within that set (ie. they are
inaccessible from Python and are garbage). We
can now go about freeing these objects.
33. Soft Reference
● The garbage collector may reclaim the
memory occupied by a softly reachable
● It’s useful for cache.
34. Weak Reference
● The garbage collector must reclaim the
memory occupied by a weakly reachable
● Canonicalizing mappings
35. Phantom Reference
● Similar with weak reference
● Whereas the garbage collector enqueues
soft and weak reference objects when their
referents are leaving the relevant
reachability state, it enqueues phantom
references when the referents are entering
the relevant state.
● Establish more flexible pre-mortem cleanup
39. In : import gc
In : gc.set_debug(gc.DEBUG_STATS)
In : gc.collect()
gc: collecting generation 2...
gc: objects in each generation: 159 2655 7538
gc: done, 10 unreachable, 0 uncollectable, 0.0123s
def __del__(self): pass
a = Finalizable()
b = Finalizable()
a.x = b
b.x = a