Converted-7da7c
Converted-7da7c
Garbage Collection
Terminology
• Stack: a memory area where
activation records or frames are
pushed onto when a procedure is
called and popped off when it returns
• Heap: a memory area where data
structures can be allocated and
deallocated in any order.
2
Terminology
(Continued)
int B()
{ int J; ... C(); A(); …}
int C()
{ int K; … B() … }
24
Why Garbage Collect?
(Continued)
• Software Engineering
– Garbage collection increases abstraction
level of software development.
– Simplified interfaces and decreases
coupling of modules.
– Studies have shown a significant amount
of development time is spent on memory
management bugs [Rovner, 1985].
25
Comparing Garbage Collection
Algorithms
• Directly comparing garbage collection algorithms
is difficult – there are many factors to consider.
• Some factors to consider:
– Cost of reclaiming cells
– Cost of allocating cells
– Storage overhead
– How does the algorithm scale with residency?
– Will user program be suspended during garbage
collection?
– Does an upper bound exist on the pause time?
– Is locality of data structures maintained (or maybe
even improved?)
26
Classes of Garbage Collection
Algorithms
• Direct Garbage Collectors: a record is
associated with each node in the heap. The
record for node N indicates how many other
nodes or roots point to N.
• Indirect/Tracing Garbage Collectors: usually
invoked when a user’s request for memory fails
because the free list is exhausted. The garbage
collector visits all live nodes, and returns all
other memory to the free list. If sufficient
memory has been recovered from this process,
the user’s request for memory is satisfied.
27
Reference count
With each dynamic structure there is a record of
how many pointers exist to that structure.
Deallocate when the reference count falls to 0.
Iterate if the deallocated structure points to
something else.
Advantage: Happens incrementally. Low cost.
Disadvantage: Doesn’t work with circular structure.
Can use:
– with structures that can’t contain pointers (e.g.
dynamic strings)
– In languages that can’t create circular structures (e.g.
restricted forms of LISP)
Mark and sweep
Any accessible structure is accessible via
some expression in terms of variables on
the stack (or symbol table etc. but some
known entity).
Therefore:
• Unmark every structure in the heap.
• Follow every pointer in the stack to
structure in the heap. Mark. Follow these
pointers. Iterate.
• Go through heap, deallocate any unmarked
structure.
Problem with Garbage Collection
(other than reference count)
Inevitably large CPU overhead.
Generally program execution has to halt
during GC. Annoying for interactive
programs; dangerous for real-time, safety
critical programs (e.g. a program to detect
and respond to meltdown in a nuclear
reactor).
Hybrid approach
Use reference counts to deallocate.