









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










CS553 Lecture Garbage Collection 1
! Last time
-! Compiling Object-Oriented Languages ! Today -! Motivation behind garbage collection -! Garbage collection basics -! Garbage collection performance -! Specific example of using GC in C++ ! Acknowledgements -! These slides are based on Kathryn McKinley’s slides on garbage collection as well as E Christopher Lewis’s slides
! Static allocation: variables are bound to storage at compile-time
-! pros: easy to implement -! cons: no recursion, data structure sizes are compile-time constants, data structures cannot be dynamic ! Stack allocation: dyn. alloc. stack frame for each proc. invocation -! pros: recursion is possible, data structure sizes may depend on parameters -! cons: stack allocated data is not persistent, stack allocated data cannot outlive the procedure for which it is defined ! Heap allocation: arbitrary alloc. and dealloc. of objects in heap -! pros: solves above problems: dynamic, persistent data structures -! cons: very difficult to explicitly manage heap
CS553 Lecture Garbage Collection 3
! Ideal (not possible)
-! deallocate all data that will not be used in the future ! What is garbage? ! Manual/Explicit -! programmer deallocates with free or delete ! Automatic/Implicit -! garbage collection
! Explicit +! efficiency can be very high +! gives programmers “control”
-! more code to maintain -! correctness is difficult -! core dump if an object is freed too soon, dangling pointers -! space is wasted if an object is freed too late -! if never free, at best waste space, at worst fail ! Automatic +! reduces programmer burden +! eliminates sources of errors +! integral to modern OOP languages (ie. Java, C#) -! can not determine all objects that won’t be used in the future -! may or may not hurt performance
CS553 Lecture Garbage Collection 7
! How it handles key issues
-! allocation is expensive because searching a freelist -! reclamation is local and incremental -! fragmentation is high ! Further analysis +! relatively simple +! very simple run-time system -! cannot reclaim cyclic data structures (shifts burden to programmer) -! high runtime overhead (must manipulate ref counts for every reference update) -! space cost -! complicates compilation
! Observation
-! rather than explicitly keep track of the number of references to each object we can traverse all reachable objects and discard unreachable objects ! Details -! start with a set of root pointers (program vars), root set -! global pointers -! pointers in stack and registers -! traverse objects recursively from root set -! visit reachable objects -! unvisited objects are garbage -! we might visit an object even if it's dynamically dead (ie, we are only conservatively approximating dead object discovery) ! When do we collect? -! when the heap is full
CS553 Lecture Garbage Collection 9
! Simple trace collector
-! trace reachable objects marking reachable objects -! sweep through all of heap -! add unmarked objects to free list -! clear marks of marked objects ! Example x y
! How it handles the key issues
-! allocation is expensive because searching a freelist -! reclamation can result in the “embarrassing pause” problem -! poor memory locality when tracing -! fragmentation is high ! Further analysis +! collects cyclic structures +! simple -! must be able to dynamically identify pointers in vars and objects -! more complex runtime system -! space overhead is only one bit per data object
CS553 Lecture Garbage Collection 13
! How it handles the key issues
-! allocation is very fast since there is no free list to search -! reclamation can result in the “embarrassing pause” problem -! poor memory locality when tracing -! copying data from one heap to another -! changing pointers because objects are being moved +! visits only reachable objects +! no fragmentation ! Further Analysis +! collects cyclic structures -! requires twice the (virtual) memory -! breadth-first traversal means to-space objects could have poor locality
! Idea
-! different collection techniques may be combined ! Example: Mark-Sweep/Copy collector -! big objects managed with mark-sweep (avoids copy time) -! small objects managed with copy collector ! Analysis +! may be more efficient -! more complex
CS553 Lecture Garbage Collection 15
! Observation
-! "young" objects are most likely to die soon, while "old“ objects are more likely to live on ! Idea -! exploit this fact by concentrating collection on "young" objects ! Details -! divide heap in generations (G0, G1, ...; G0 for youngest objects) -! collect G0 most frequently, G1 less frequently, etc. -! object is “tenured” from one gen. to next after surviving several GCs ! Result -! usually only have to collect a small sub-heap
! Additional issues
-! need to encode “age” in object -! root set for objects in one generation may come from another gen. -! generation Gi should be k times larger than Gi- -! each generation may be collected with different algorithm ! Dealing with cross-generation pointers -! older to younger ( i.e., Gi to Gj for i>j) are uncommon -! search all of Gi? -! write barriers -! younger to older ( i.e., Gj to Gi for i>j) are very common -! collect Gj when collecting Gi
CS553 Lecture Garbage Collection 19
! Generally
-! When allocation is no longer possible, garbage collection is necessary -! VM usually stops all mutator threads -! JIT generated code must properly handle out-of-memory exception ! Write barriers (for reference counting and generational collection) -! Each time a write to a pointer occurs, a write barrier catches this and performs some action -! generation collection needs the write barrier to keep track of pointers from older generations to new generations -! reference counting requires write barriers to detect when any pointer changes “...Performance Impact of Garbage Collection” [Blackburn et al 2004] ! Experiment setup -! use Jikes RVM (research virtual machine), highly optimized -! MMtk (The Memory Management Toolkit) is a framework for construction of garbage collectors within Jikes RVM. -! Machines: Athlon (best performance), Pentium 4, Power PC -! Benchmarks: SPEC JVM benchmarks and pseudojbb (variant of SPEC JBB2000) -! garbage collection algorithms -! semi-space, a copying tracing collector -! mark-sweep -! reference-counting
CS553 Lecture Garbage Collection 21
! Contiguous allocation is better than free-list allocators
-! mutator performance is 5-15% better due to improved data locality ! Generational collectors are better than whole heap collectors -! write barrier overhead is only (2-14%) and is outweighed by improvements in collection time ! Tracing collection is better than reference counting -! the overhead of reference counting is too expensive -! reference counting can be beneficial for older generations of the heap ! Nursery size in generational collector should be about 4-8MB -! debunks the myth that the size should be about L2 cache size (512KB) -! have to get to the point where constant number of roots (about 64KB)
! OpenAnalysis
-! goal is to do program analysis of large programs, therefore can’t just leak memory -! explicit management is very error prone -! difficult to debug segfaults -! for analysis results it isn’t clear which objects should own which other objects, therefore an explicit management policy proved very problematic ! Options -! use one of the conservative garbage collectors -! have portability issues -! smart pointers
CS553 Lecture Garbage Collection 25
! Allowing polymorphism OA_ptr
! Want to pass in a subclass to a base class int foo( OA_ptr
CS553 Lecture Garbage Collection 27
! Decided against raw pointer comparison to catch more static errors OA_ptr<Location::Location> loc = r->foo(mre); if (!loc.ptrEqual(NULL)) { ... } ! Returning an OA_ptr ! OA_ptr
! Categorizing garbage collection algorithms
-! how is garbage identified? reference counting or tracing -! when is it collected? incrementally, generationally, or all at once -! what allocator is used? contiguous/bump allocator or freelist -! what mechanism is used for reclamation? copying or put on freelist -! how is the heap space managed? split for copying or generations? ! Open problems in garbage collection -! still no conclusive evidence that it is always faster or slower than explicit memory management -! how can we measure whether it is or not?