9051628a8dc3d32290512dd5fa0de8c7.ppt
- Количество слайдов: 21
A Rapid Introduction to Garbage Collection mm-net Garbage Collection & Memory Management Summer School Tuesday 20 July 2004 Richard Jones Computing Laboratory University of Kent at Canterbury © Richard Jones, 2004. All rights reserved. © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 1
PART 1: Introduction Motivation for garbage collection What to look for © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 2
Why garbage collect? Finite storage requirement • computer have finite, limited storage Language requirement • many OO languages assume GC, e. g. allocated objects may survive much longer than the method that created them Problem requirement • the nature of the problem may make it very hard/impossible to determine when something is garbage © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 3
Why automatic garbage collection? Because human programmers just can’t get it right. Either too little is collected leading to memory leaks, or too much is collected leading to broken programs. Explicit memory management conflicts with the software engineering principles of abstraction and modularity. It’s not a silver bullet • Some memory management problems cannot be solved using automatic GC, e. g. if you forget to drop references to objects that you no longer need. • Some environments are inimical to garbage collection – embedded systems with limited memory – hard real-time systems © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 4
PART 2: The Basics • What is garbage? • The concept of liveness by reachability • The basic algorithms • The cost of garbage collection © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 5
What is garbage? Almost all garbage collectors assume the following definition of live objects called liveness by reachability: if you can get to an object, then it is live. More formally: An object is live if and only if: it is referenced in a predefined variable called a root, or it is referenced in a variable contained in a live object (i. e. it is transitively referenced from a root). Non-live objects are called dead objects, i. e. garbage. © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 6
Roots Objects and references can be considered a directed graph. Live objects are those reachable from a root. A process executing a computation is called a mutator — it simply modifies the object graph dynamically. Determining roots of a computation is, in general, language-dependent. In common language implementations roots include • • • words in the static area registers words on the execution stack that point into the heap. © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 7
The basic algorithms • Reference counting: Keep a note on each object in your garage, indicating the number of live references to the object. If an object’s reference count goes to zero, throw the object out (it’s dead). • Mark-Sweep: Put a note on objects you need (roots). Then recursively put a note on anything needed by a live object. Afterwards, check all objects and throw out objects without notes. • Mark-Compact: Put notes on objects you need (as above). Move anything with a note on it to the back of the garage. Burn everything at the front of the garage (it’s all dead). • Copying: Move objects you need to a new garage. Then recursively move anything needed by an object in the new garage. Afterwards, burn down the old garage (any objects in it are dead)! © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 8
Reference counting The simplest form of garbage collection is reference counting. Basic idea: count the number of references from live objects. Each object has a reference count (RC) • when a reference is copied, the referent’s RC is incremented • when a reference is deleted, the referent’s RC is decremented • an object can be reclaimed when its RC = 0 Update(left(R), S) © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 9
Advantages of reference counting O H P ü Simple to implement ü Costs distributed throughout program ü Good locality of reference: only touch old and new targets' RCs ü Works well because few objects are shared and many are short-lived ü Zombie time minimized: the zombie time is the time from when an object becomes garbage until it is collected ü Immediate finalisation is possible (due to near zero zombie time) © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 10
Disadvantages of reference counting O H P û Not comprehensive (does not collect all garbage): cannot reclaim cyclic data structures û High cost of manipulating RCs: cost is ever-present even if no garbage is collected û Bad for concurrency — need Compare&Swap û Tightly coupled interface to mutator û High space overheads û Recursive freeing cascade © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 11
Mark-Sweep Mark-sweep is a tracing algorithm — it works by following (tracing) references from live objects to find other live objects. Implementation: Each object has a mark-bit associated with it. There are two phases: • Mark phase: starting from the roots, the graph is traced and the mark-bit is set in each unmarked object encountered. At the end of the mark phase, unmarked objects are garbage. • Sweep phase: starting from the bottom, the heap is swept – mark-bit not set: the object is reclaimed – mark-bit set: the mark-bit is cleared © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 12
A simple mark-sweep example 0 2 1 © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 13
O H P Advantages of mark-sweep ü Comprehensive: cyclic garbage collected naturally ü No run-time overhead on pointer manipulations ü Loosely coupled to mutator ü Does not move objects • does not break any mutator invariants • optimiser-friendly • requires only one reference to each live object to be discovered (rather than having to find every reference) © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 14
O H P Disadvantages of mark-sweep û Stop/start nature leads to disruptive pauses and long zombie times. û Complexity is O(heap) rather than O(live) • every live object is visited in mark phase • every object, alive or dead, is visited in sweep phase û Degrades with residency (heap occupancy) • the collector needs headroom in the heap to avoid thrashing û Fragmentation and mark-stack overflow are issues û Tracing collectors must be able to find roots (unlike reference counting) © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 15
Fast allocation? Problem: Non-moving memory managers fragment the heap • mark-sweep • reference counting A compacted heap • offers better spatial locality, e. g. better virtual memory and cache performance • allows fast allocation – merely bump a pointer © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 16
Copying GC Example copy root scan C' B' A' scan D' and E' scan=free scan G' F' and update pointer, copy F and G, copy B and C, D E, use A'snothingis complete so collection to do forwarding address leaving forwarding addresses leaving forwarding address © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 18
Disadvantages of copying GC û Stop-and-copy may be disruptive Degrades with residency û Requires twice the address space of other simple collectors • touch twice as many pages • trade-off against fragmentation û Cost of copying large objects Long-lived data may be repeatedly copied û All references must be updated Moving objects may break mutator invariants û Breadth-first copying may disturb locality patterns © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 20
Mark-compact collection Mark-compact collectors make at least two passes over the heap after marking • to relocate objects • to update references (not necessarily in this order) Issues • how many passes? • compaction style – sliding: preserve the original order of objects – linearising: objects that reference each other are placed adjacently (as far as possible) – arbitrary: objects moved without regard for original order or referential locality © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 21
Cost metrics Many cost metrics can be interesting (albeit not necessarily at the same time). These cost metrics cover different types of concerns that may apply. The metrics are partially orthogonal, partially overlapping, and certainly also partially contradictory. In general it is not possible to identify one particular metric as the most important in all cases — it is application dependent. Because different GC algorithms emphasise different metrics, it is also, in general, not possible to point out one particular GC algorithm as “the best”. In the following, we present the most important metrics to consider when choosing a collector algorithm. © Richard Jones, Eric Jul, 1999 -2004 mmnet GC & MM Summer School, 20 -21 July 2004 22
GC Metrics Execution time Delay time • total execution time • distribution of GC execution time • time to allocate a new object Memory usage • additional memory overhead • fragmentation • virtual memory and cache performance © Richard Jones, Eric Jul, 1999 -2004 • length of disruptive pauses • zombie times Other important metrics • comprehensiveness • implementation simplicity and robustness mmnet GC & MM Summer School, 20 -21 July 2004 23
9051628a8dc3d32290512dd5fa0de8c7.ppt