3. Motivation
Managing memory manually is hard.
Who owns the memory?
Should I free these resources?
What happens with double frees?
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 3 / 30
4. Dangling pointers
int * func ( void )
{
int num = 1234;
/* ... */
return &num ;
}
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 4 / 30
6. Basic concepts
Heap
A data structure in which objects may be allocated or deallocated in any
order.
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 6 / 30
7. Basic concepts
Heap
A data structure in which objects may be allocated or deallocated in any
order.
Mutator
The part of a running program which executes application code.
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 7 / 30
8. Basic concepts
Heap
A data structure in which objects may be allocated or deallocated in any
order.
Mutator
The part of a running program which executes application code.
Collector
The part of a running program responsible of garbage collection.
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 8 / 30
10. nition
Garbage collection is automatic memory management. While the
mutator runs , it routinely allocates memory from the heap. If more
memory than available is needed, the collector reclaims unused memory
and returns it to the heap.
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 9 / 30
11. CPython GC
CPython implementation has garbage collection.
CPython GC algorithm is Reference counting with cycle detector
It also has a generational GC.
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 10 / 30
12. Young objects
[ elem * 2 for elem in elements ]
balance = (a / b / c) * 4
'asdadsasd - xxx '. replace ('x', 'y'). replace ('a', 'b')
foo.bar ()
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 11 / 30
25. Reference counting
Pros: Is incremental, as it works, it frees memory.
Cons: Detecting Cycles could be hard.
Cons: Size overhead on objects.
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 24 / 30
27. Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 26 / 30
28. Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 27 / 30
29. Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 28 / 30
30. Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 29 / 30
31. Mark and sweep
Pros: Can collect cycles.
Cons: Basic implementation stops the world
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 30 / 30