Automatic Memory Management
- The concept is implemented when program runs out of space, in this case memory is freed and used.
- To free up memory, unreachable objects can be deallocated. Unreachable objects are those which are not referenced by any other pointer.
- To find those unreachable memory objects, list the root registers and then find all the connections to others pointers, connect to these pointer’s pointers. Free up space for the not connected object.
X ← new A; Y ← new B; X ← Y; //Memory allocated to X cab be freed if 1 then X ← new A else ... fi //if Y is not used again then space for B is also unused although it is being reference.
if a variable is dead, then memory can surely be used and if it is not then possibly it can be used.
- Most of the garbage collectors do it in this way:
Allocate as much as unused space if program runs out off space, then run garbage collector compute all the reachable objects from the root registers free space if not reachable
Some strategy may perform garbage collection before running out of space.
- See video for a better explanation.