automatic memory management
reference counting
reference counting
- every variable has a reference-count(RC) field
- RC is the number of references to the variable
- when RC reaches 0 the variable is deallocated
Object a = new Object(); // let's call it A
// RC(A) = 1
Object b = new Object(); // let's call it B
// RC(B) = 1
b = a;
// RC(A) = 2
// RC(B) = 0 -> B is deallocated
pros
- predictable performance
- smooth execution without interruptions
- implementable in:
- manual memory management system
- automatic memory management system
- cost is proportional to actual computation
cons
- cannot deal with circular structures
- is generally slow incurring a huge “write barrier”
garbage collection
garbage collection
deallocation is the responsibility of the language runtime system, not the programmer’s
- programmer never deallocates memory
- a GC procedure collects unused variables from time to time
- GC is found in: Java, Python, ML, Lisp, …
mark & sweep
- mark unmark all cells
- sweep mark all cells in use (stack, global variables), and cells which can be accessed (directly or indirectly) from them
- release release all unmarked cells