Garbage Collection and Memory Management CS 480/680 – Comparative Languages.

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.
Compilation /15a Lecture 13 Compiling Object-Oriented Programs Noam Rinetzky 1.
Stack Frames: Using LINK. CEG 320/5208: Stack frames and LINK2 Assembling source code One source file: –Use ORG statements for data and code –Assemble.
Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Memory Management Tom Roeder CS fa. Motivation Recall unmanaged code eg C: { double* A = malloc(sizeof(double)*M*N); for(int i = 0; i < M*N; i++)
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
CS 536 Spring Automatic Memory Management Lecture 24.
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.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Garbage Collection Mooly Sagiv html://
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Run time vs. Compile time
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.
Compilation 2007 Garbage Collection Michael I. Schwartzbach BRICS, University of Aarhus.
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.
Garbage collection (& Midterm Topics) David Walker COS 320.
Linked lists and memory allocation Prof. Noah Snavely CS1114
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.
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.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
File I/O Applied Component-Based Software Engineering File I/O CSE 668 / ECE 668 Prof. Roger Crawfis.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
Incremental Garbage Collection Uwe Kern 23. Januar 2002
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
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.
CSC 213 – Large Scale Programming. Explicit Memory Management  Traditional form of memory management  Used a lot, but fallen out of favor  malloc /
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.
Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory.
® 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.
Reference Counting. Reference Counting vs. Tracing Advantages ✔ Immediate ✔ Object-local ✔ Overhead distributed ✔ Very simple Trivial implementation for.
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.
GARBAGE COLLECTION Student: Jack Chang. Introduction Manual memory management Memory bugs Automatic memory management We know... A program can only use.
Garbage Collection What is garbage and how can we deal with it?
Dynamic Compilation Vijay Janapa Reddi
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Smalltalk Implementation: Memory Management and Garbage Collection
Rifat Shahriyar Stephen M. Blackburn Australian National University
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Automatic Memory Management
Smart Pointers.
Strategies for automatic memory management
Memory Management Kathryn McKinley.
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Automating Memory Management
Reference Counting.
CMPE 152: Compiler Design May 2 Class Meeting
Garbage Collection What is garbage and how can we deal with it?
Mooly Sagiv html:// Garbage Collection Mooly Sagiv html://
Reference Counting vs. Tracing
Presentation transcript:

Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection2 Motivation for GC int mysub(times) { myClass *myObject; myObject = new myClass; for(int i=0; i<times; i++) { int a = myObject.firstMethod(); int b = myObject.secondMethod(); } return(a/b); } What’s wrong with this code?

Garbage Collection3 Memory Management  Explicit memory management C++new/delete Cmalloc/free  Garbage collection Javanew Class RubyClass.new Perl Smalltalk Etc…

Garbage Collection4 What is Garbage Collection  Ideally, the garbage collector will find all objects that will not be accessed in the future.  Of course, this is impossible, but a good guess is those objects that have no references to them remaining in scope.  When to invoke GC? When memory is low When the local scope changes When the system is idle When a reference is destroyed Others?

Garbage Collection5 Advantages of Garbage Collection  Easier on the programer Create objects when needed No need to de-allocate space No need for destructors and other special memory considerations  Less errors Less memory leaks No dangling pointers

Garbage Collection6 GC Strategies  Two of the most common strategies are: Tracing  Includes the very common Mark & Sweep method Reference Counting

Garbage Collection7 Tracing Garbage Collectors  Start with all objects accessible to the main program (called roots) Registers Stack Instruction pointer Global variables  Find all objects reachable from the roots

Garbage Collection8 Roots D0 D1 D2 A0 A1 A7 … … $7000 ? rtrn $6FFE $6FFC Old A6 var1 var2 param $6FFA $6FF8 $6FF6 $6FF4 $6FF2 $6FF0 $6FEE $6FEC $6FEA $6FE8 ptr1 ptr2 Global variable list PC

Garbage Collection9 Reference Chains $7000 ? rtrn $6FFE $6FFC Old A6 var1 var2 param $6FFA $6FF8 $6FF6 $6FF4 $6FF2 $6FF0 $6FEE $6FEC $6FEA $6FE8 ptr1 ptr2 myObject acctList Value Next

Garbage Collection10 Tri-color Algorithm  Every object is the system is “marked” in one of three colors: White – candidate for recycling (condemned) Black – Safe from recycling; No references to objects in the white set Grey – Safe from recycling; May refer to objects in the white set

Garbage Collection11 Initialization  White set – everything except the roots  Black set – empty  Grey set – the roots (already safe from recycling)

Garbage Collection12 Tracing Repeat: 1.Pick an object from the grey set 2.Grey all white objects that this object refers to 3.Move the object to the black set Until: The grey set is empty Now recycle all objects remaining in the white set.

Garbage Collection13 Example Step (1) $7000 ? rtrn $6FFE $6FFC Old A6 var1 var2 param $6FFA $6FF8 $6FF6 $6FF4 $6FF2 $6FF0 $6FEE $6FEC $6FEA $6FE8 ptr1 ptr2 Value Next Value Next

Garbage Collection14 Example Step (2) $7000 ? rtrn $6FFE $6FFC Old A6 var1 var2 param $6FFA $6FF8 $6FF6 $6FF4 $6FF2 $6FF0 $6FEE $6FEC $6FEA $6FE8 ptr1 ptr2 Value Next Value Next

Garbage Collection15 Example Step (3) $7000 ? rtrn $6FFE $6FFC Old A6 var1 var2 param $6FFA $6FF8 $6FF6 $6FF4 $6FF2 $6FF0 $6FEE $6FEC $6FEA $6FE8 ptr1 ptr2 Value Next Value Next

Garbage Collection16 Example Step (4) $7000 ? rtrn $6FFE $6FFC Old A6 var1 var2 param $6FFA $6FF8 $6FF6 $6FF4 $6FF2 $6FF0 $6FEE $6FEC $6FEA $6FE8 ptr1 ptr2 Value Next Value Next

Garbage Collection17 An Important Observation  Note that, by following this method, an object in the black set can never refer to an object in the white set  When an object is moved to the black set, all the objects it points to are moved to the grey set.  At the end, all of the objects in the white set have no live references to them, and can safely be removed.

Garbage Collection18 Variations of Tracing GC  To move or not to move… Mark & Sweep – keep a few bits with each object to indicate if it is in the black, grey, or white sets. After marking, recycle every object in memory in the white set. Copying GC – relocate objects in the black set to a “safe” area of memory, then recycle everything else  This is nice for creating good locality of reference!  Also reduces fragmentation.  Can be slower.

Garbage Collection19 More Variations  Can the collector identify which parts of objects are references (pointers) and which are not? Yes: “precice” GC No: “conservative” GC  What if pointers are encrypted, scrambled, or stored in some other “funny” way? Value Next Value Next

Garbage Collection20 Still More Variations  Can the GC mechanism run incrementally? No: stop the rest of the system, do GC, then start up again Yes: interleave their work with work units from the rest of the system  Can the GC mechanism run in a parallel thread?  Note: All methods must at least scan the root set all at once Why?

Garbage Collection21 Generational GC  It is not strictly necessary to place everything (except the root objects) in the white set Faster GC can be performed by limiting the size of the white set What should go there?  Statistically speaking, the newest objects are also the most likely to go out of scope soon Referred to as infant mortality or the generational hypothesis Divide run time into generations, only put objects created in the current generation into the white set

Garbage Collection22 Disadvantages of Tracing GC  Can be invoked at any time, and can be slow Not a good thing for Real Time (RT) computing  The GC thread violates locality of reference by intentionally looking at memory areas that haven’t been accessed recently So what?

Garbage Collection23 Open Research  Is all memory that is reachable still in use?  Consider ARGV & ARGC. Usually read, parsed, and then ignored for the rest of the process. Do we really need to keep it around?  Research area: finding objects that will never be used again…

Garbage Collection24 Reference Counting  Keep a reference count with every object  Advantages: Easy to implement Fast and incremental GC  Disadvantages: Must update reference count whenever a reference is created or deleted: SLOW Need extra space in every object Does not reclaim space if there are reference cycles (as in a doubly linked list)