Reference Counting. Reference Counting vs. Tracing Advantages ✔ Immediate ✔ Object-local ✔ Overhead distributed ✔ Very simple Trivial implementation for.

Slides:



Advertisements
Similar presentations
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
(Chapter 5) Deleting Objects
Compilation /15a Lecture 13 Compiling Object-Oriented Programs Noam Rinetzky 1.
1 Overview Assignment 5: hints  Garbage collection Assignment 4: solution.
On-the-Fly Garbage Collection Using Sliding Views Erez Petrank Technion – Israel Institute of Technology Joint work with Yossi Levanoni, Hezi Azatchi,
Incorporating Generations into a Modern Reference Counting Garbage Collector Hezi Azatchi Advisor: Erez Petrank.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Copying GC and Reference Counting Jonathan Kalechstain Tel Aviv University 11/11/2014.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
An On-the-Fly Mark and Sweep Garbage Collector Based on Sliding Views Hezi Azatchi - IBM Yossi Levanoni - Microsoft Harel Paz – Technion Erez Petrank –
Efficient Concurrent Mark-Sweep Cycle Collection Daniel Frampton, Stephen Blackburn, Luke Quinane and John Zigman (Pending submission) Presented by Jose.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Increasing Memory Usage in Real-Time GC Tobias Ritzau and Peter Fritzson Department of Computer and Information Science Linköpings universitet
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
Using Prefetching to Improve Reference-Counting Garbage Collectors Harel Paz IBM Haifa Research Lab Erez Petrank Microsoft Research and Technion.
1 The Compressor: Concurrent, Incremental and Parallel Compaction. Haim Kermany and Erez Petrank Technion – Israel Institute of Technology.
Garbage Collection Mooly Sagiv html://
An On-the-Fly Reference Counting Garbage Collector for Java Erez Petrank Technion – Israel Institute of Technology Joint work with Yossi Levanoni – Microsoft.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Incremental Garbage Collection
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Age-Oriented Concurrent Garbage Collection Harel Paz, Erez Petrank – Technion, Israel Steve Blackburn – ANU, Australia April 05 Compiler Construction Scotland.
1 An Efficient On-the-Fly Cycle Collection Harel Paz, Erez Petrank - Technion, Israel David F. Bacon, V. T. Rajan - IBM T.J. Watson Research Center Elliot.
Damien Doligez Georges Gonthier POPL 1994 Presented by Eran Yahav Portable, Unobtrusive Garbage Collection for Multiprocessor Systems.
Uniprocessor Garbage Collection Techniques Paul R. Wilson.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Compiler Optimizations for Nondeferred Reference-Counting Garbage Collection Pramod G. Joisha Microsoft Research, Redmond.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Garbage Collection Memory Management Garbage Collection –Language requirement –VM service –Performance issue in time and space.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Taking Off The Gloves With Reference Counting Immix
Chapter 7 Run-Time Environments
Ulterior Reference Counting: Fast Garbage Collection without a Long Wait Author: Stephen M Blackburn Kathryn S McKinley Presenter: Jun Tao.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Incremental Garbage Collection Uwe Kern 23. Januar 2002
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Compilation (Semester A, 2013/14) Lecture 13b: Memory Management Noam Rinetzky Slides credit: Eran Yahav 1.
Fast Garbage Collection without a Long Wait Steve Blackburn – Kathryn McKinley Presented by: Na Meng Ulterior Reference Counting:
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
More Distributed Garbage Collection DC4 Reference Listing Distributed Mark and Sweep Tracing in Groups.
Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
® July 21, 2004GC Summer School1 Cycles to Recycle: Copy GC Without Stopping the World The Sapphire Collector Richard L. Hudson J. Eliot B. Moss Originally.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
An Efficient, Incremental, Automatic Garbage Collector P. Deutsch and D. Bobrow Ivan JibajaCS 395T.
Naming CSCI 6900/4900. Unreferenced Objects in Dist. Systems Objects no longer needed as nobody has a reference to them and hence will not use them Garbage.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Garbage Collection What is garbage and how can we deal with it?
Smalltalk Implementation: Memory Management and Garbage Collection
Rifat Shahriyar Stephen M. Blackburn Australian National University
Concepts of programming languages
Cycle Tracing Chapter 4, pages , From: "Garbage Collection and the Case for High-level Low-level Programming," Daniel Frampton, Doctoral Dissertation,
Ulterior Reference Counting Fast GC Without The Wait
Simulated Pointers.
Strategies for automatic memory management
Memory Management Kathryn McKinley.
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
CS703 - Advanced Operating Systems
Reference Counting.
Garbage Collection What is garbage and how can we deal with it?
Reference Counting vs. Tracing
Presentation transcript:

Reference Counting

Reference Counting vs. Tracing Advantages ✔ Immediate ✔ Object-local ✔ Overhead distributed ✔ Very simple Trivial implementation for naïve RC Disadvantages ✘ Maintain count Time and space overheads ✘ Cycles Can’t be collected ✘ Complex High performance implementation about as complex as tracing 2

Garbage collection is ubiquitous Two ideas underpin large literature: -Tracing [McCarthy60] -Reference Counting [Collins60] However ✔ Tracing used in all high performance GCs ✘ Reference counting only in non-performance critical settings 3

Reference counting [Collins 1960] Associate a reference count field with each object – how many pointers reference this object When nothing points to an object, it can be deleted Decrement count for all descendants of dead objects 4

Basic Reference Counting 5 ABCDEF E 01

Write Barriers Small piece of code Used to intercept pointer writes Generated by the compiler 6

Cycles? [Harold-McBeth 1963] Reference counting algorithm does not reclaim cycles! But, it turns out that “typical” programs do not use too many cyclic structures – This is correct also for modern benchmarks (including SPECjbb2000) So, other methods are used “seldom” to collect the cycles 7

Lazy Freeing [Weizenbaum, 1963] Problem: uneven processing overheads. – Cost of deleting last pointer to an object O depends on size of sub-graph rooted at O Solution: lazy freeing – Free lazily with a stack – When last pointer to O deleted: push O to stack – During allocation: pop O from stack, free O, decrement rc for children of O – If any got down to 0 – push it to the stack 8

Lazy Freeing Properties Advantage – Splitting the costs of deletion evenly throughout the computation – Efficiency (almost) unchanged: deletion is just spread into many allocation operations Disadvantages: – Complication of the allocation process – It may take time to realize that a large consecutive space has been freed 9

Sticky reference counts Problem: Space overhead for ref counters – Theoretically, large enough to hold number of pointers in the heap Idea: use small rc fields (may overflow) – When rc overflows, think of it as stuck – Stuck rc’s are not decremented/incremented – To reclaim objects with stuck rc, their rc values must be restored 10

Sticky reference counts 11

Restoring reference counts Auxiliary data structures – to store count of the overflowed objects – Hash Table can be used Cycle collector – Ignore them (let backup tracing collect stuck objects) – Restore them (let backup tracing restore stuck counts) 12

Deferred Reference Counting Problem: overhead on updating changes to stacks and registers is too high Solution [Deutch & Bobrow, 1976] – Don’t update rc for stacks and registers – rc’s reflect only references from heap objects not from stack – We can’t delete objects whose rc drops to zero – Instead objects with rc = 0 are pushed to ZCT (Zero Count Table) – “Once in a while”: collect all objects with rc=0 that are not referenced from stacks and registers 13

Deferred RC Properties Advantages: – Deferred RC reduces overhead by 80%. Disadvantages: – Immediacy of collection lost ! – Space overhead for ZCT. Used in most modern RC systems 14

Deferral [Deutsch and Bobrow 1976, Bacon et al. 2001] Stacks & Registers A++ F++ B-- 15 D++ A-- F-- A BCDFE A mutator activityGC: scan rootsGC: apply incrementsGC: apply decrementsGC: collectGC: move deferred decs A-- F '

Coalescing [Levanoni and Patrank 2001] B-- Remember AIgnore intermediate mutations Compare A, A old B--, F++ C++ C-- D++ D-- E++ E-- F++ 16 ABCDFE

Cycles in Reference Counting Systems Reference counting collectors employ one of 2 avenues to collect garbage cycles: – A backup tracing collector Trace all live objects and sweep the entire heap Can also restore reference counts – A cycle collector Known as trial deletion 17

Cycle Collection Basic Idea - 1 Observation 1: – Garbage cycles can only be created when a rc is decremented to a non-zero value – Objects whose rc is decremented to a non-zero value become candidates 18

Cycle Collection Basic Idea - 2 Sub-graph of O – graph of objects reachable from O External pointer (to a sub-graph) – a pointer from a non sub-graph object to a sub-graph object Internal pointer (of a sub-graph) – a pointer between 2 sub-graph objects 19 o o1o2 o4o5 a o3

Cycle Collection Basic Idea - 2 Observation 2: – In a garbage cycle all the reference counts are due to internal pointer of the cycle – For each candidate’s sub-graph, check if external pointers point to this sub-graph 20

21 Counts for External Pointers Only 1 r 2 a 1 b c 2 d 2 Not a garbage cycle a garbage cycle 1 r 1 a 1 b c 2 d 2 edge r->a deleted 1 r 0 a 0 b c 0 d 1 rc ’ s when ignoring internal edges

Implementing the Cycle Collector Idea Object is colored black/gray/ white. Whenever rc of an object ‘a’ is decremented to a non-zero, perform local traversals over the graph of objects of ‘a’. Mark: Updates rc’s to reflect only pointers that are external to the graph of ‘a’, marking nodes in gray Scan: Restores rc’s of the externally reachable objects, coloring them in black. Rest of the nodes are white. Collect: collects garbage objects (the white ones) r 2 a 1 b c 2 d