570 likes | 693 Views
Compile-Time Deallocation of Individual Objects Sigmund Cherem and Radu Rugina. International Symposium on Memory Management June, 2006. Motivation. Memory management challenges Effective reclamation of memory (precision) Transparent to users and applications
E N D
Compile-Time Deallocation of Individual ObjectsSigmund Cherem and Radu Rugina International Symposium on Memory Management June, 2006
Motivation • Memory management challenges • Effective reclamation of memory (precision) • Transparent to users and applications • Eliminate memory errors (safety) • Minimize run-time overhead (efficiency) • Support real-time systems (predictability) • Automatic memory management • Eliminate user’s responsibilities on MM
Overhead Precision Safety
Overhead No MM Precision Safety
Overhead Manual MM Precision Safety
Overhead Dynamic MM Precision Safety
Overhead Dynamic MM Compile-Time MM Precision Safety
Overhead Dynamic MM Hybrid MM Compile-Time MM Precision Safety
Compile-Time MM • General Idea • Estimate object lifetimes • Reachability: objects reachable from stack variables • Ref-counts: objects with some references into them • Liveness: objects being used • Transform programs to deallocate objects • Regions: deallocate entire group of heap objects • Stack allocation: place objects on stack frames • Free statements: deallocate individual objects • Our Approach • Reference count based reachability • Reclaim individual objects using “free” statements • Flexibility, simplicity and efficiency
Running Example class L { D d; L n; } class D { int val; } L removeVal (L x, int v) { … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; } … return x; }
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; }
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; } p c n n n … … d d d
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; } n p c n n … … d d d
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; } n p c n … … d d d
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; } n p c n … … d d d
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; } n p c n … … d d d
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; } n p c n … … d d d
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … free(c); ? c = p.n; } n p c n … … d d d
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … free(c); ? c = p.n; } n p c n … … d d d p,c n n n … … d d d
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … if (p != c) free(c); c = p.n; } n p c n … … d d d p,c n n n … … d d d
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … if (p != c) free(c); c = p.n; } n p c n … … d d d
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … if (p != c){ free(c.d); free(c); } c = p.n; } n p c n … … d d d
Jfree Compiler Reference Count Analysis Insert Free Statements Java Bytecode Java Bytecode + Free
Reference Count Analysis Insert Free Statements Java Bytecode Java Bytecode + Free
Reference Count Analysis • Determines when objects become unreachable • Compute ref-counts for each object at each program point • Track each object to determine its reference counts: Tracked Object Analysis • Precision: able to distinguish between object instances • Scalability at the inter-procedural level: large reuse of information on method summaries • Extends shape analysis with local reasoning [HR POPL’05] • Taking advantage of type safety in Java • Modeling Java programming practices or idioms
Reference Count Analysis • Example concrete memory • Abstract representation • Use one unit (configuration) to represent individual objects • Use heap reference counts, hit expressions, and more information about the object • Each unit tracked independently x y n n n d d d d x
Reference Count Analysis • Example concrete memory • Abstract representation • Use one unit (configuration) to represent individual objects • Use heap reference counts, hit expressions, and more information about the object • Each unit tracked independently x y n n n d d d d x y, n1
Reference Count Analysis • Example concrete memory • Abstract representation • Use one unit (configuration) to represent individual objects • Use heap reference counts, hit expressions, and more information about the object • Each unit tracked independently x y n n n d d d d x y, n1 n1
Reference Count Analysis • Example concrete memory • Abstract representation • Use one unit (configuration) to represent individual objects • Use heap reference counts, hit expressions, and more information about the object • Each unit tracked independently x y n n n d d d d x y, n1 n1
Reference Count Analysis • Example concrete memory • Abstract representation • Use one unit (configuration) to represent individual objects • Use heap reference counts, hit expressions, and more information about the object • Each unit tracked independently x y n n n d d d d x y, n1 n1 d1
Reference Count Analysis • Example concrete memory • Abstract representation • Use one unit (configuration) to represent individual objects • Use heap reference counts, hit expressions, and more information about the object • Each unit tracked independently x y n n n d d d d x y, n1 n1 d1
Reference Count Analysis • Example concrete memory • Abstract representation • Use one unit (configuration) to represent individual objects • Use heap reference counts, hit expressions, and more information about the object • Each unit tracked independently x y n n n d d d d x y, n1 (x.n) n1 d1
Running Example … while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; c, n1 (p.n) p c … …
Running Example … while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; c, n1 (p.n) c p c … …
Running Example … while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; c, n1 (p.n) c c p c … …
Running Example … while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; c, n1 (p.n) c c c p c … …
Running Example … while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; c, n1 (p.n) c c c p c … …
Running Example … while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … /* free(c); */ c = p.n; c, n1 (p.n) c c c p c … …
Other Analysis Details (I) Abstraction Elements • More than reference counts and hit expressions • Miss expressions: expressions that don’t reference the object • Expression equivalences: alias information about neighbors of tracked object Intra-procedural Analysis • Dataflow analysis that starts at allocation sites • Updates configurations on assignments • Merges some configurations at merge points
Other Analysis Details (II) Inter-procedural Analysis • Split configuration in halves • Filter non-accessed information • No need to analyze a portion not used by the callee • Skip call if accessed portion is empty • Add extended parameters • Helps preserving precise information about equivalences • Analyze callee • Record and use method summaries • Summaries are independent of caller information • Combine • Recover and merge non-accessed information
Reference Count Analysis Insert Free Statements Java Bytecode Java Bytecode + Free
Transformation • Goal : automatically insert free statements to reclaim dead objects • Technique • At points where tracked object becomes unreachable • Extract expression from responsible statement e = g; free(e); • Determine conditions for deallocation (discriminants) • Look for hit expressions in other configurations • Find a set of expressions that distinguishes them
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … c = p.n; } c, n1 (p.n) c c, p, n1
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … if ( ??? ) { free(c); } c = p.n; } c, n1 (p.n) c c, p, n1
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … if (p != c) { free(c); } c = p.n; } c, n1 (p.n) c c, p, n1
Reference Count Analysis Insert Free Statements Java Bytecode Java Bytecode + Free
Reiteration • Model a cascade effect of deallocations • Deallocate multiple level structures • Model the semantics of deallocation statements • Temporarily add nullifications into code free(x) x.f = null; • No need to reanalyze whole program • Cleanup and recompute affected part • Limited to finite number of iterations • Can’t deallocate recursive structures
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … if (p != c) { c.d = null; free(c); } c = p.n; }
Running Example … L p = x, c = p.n; while (c != null) { if (c.d.val == v) { p.n = c.n; c.n = null; } else p = c; … if (p != c) { free(c.d); free(c); } c = p.n; }
Analysis Enhancements • Variable Equality Analysis • Infer additional equivalences that exist prior to analysis starting point • Variable Liveness Analysis • Increases precision by reducing object lifetimes • Allows to deallocate objects earlier • Variable Uniqueness Analysis • No need for conditional deallocation: decide deallocation in isolation • Escape/effect analysis • Accelerate inter-procedural analysis by improving filtering of information and skipping of calls