Garbage Collection CS 537 - Introduction to Operating Systems.

Slides:



Advertisements
Similar presentations
(Chapter 5) Deleting Objects
Advertisements

Chapter 2: Memory Management, Early Systems
Memory Management, Early Systems
Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free.
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++)
Garbage Collection. Memory Management So Far ● Some items are preallocated and persist throughout the program: ● What are they? Some are allocated on.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Locality-Conscious Lock-Free Linked Lists Anastasia Braginsky & Erez Petrank 1.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
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)
CS510 Advanced OS Seminar Class 10 A Methodology for Implementing Highly Concurrent Data Objects by Maurice Herlihy.
CS 333 Introduction to Operating Systems Class 18 - File System Performance Jonathan Walpole Computer Science Portland State University.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Garbage collection (& Midterm Topics) David Walker COS 320.
Linked lists and memory allocation Prof. Noah Snavely CS1114
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.
Memory Allocation CS Introduction to Operating Systems.
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.
Ulterior Reference Counting: Fast Garbage Collection without a Long Wait Author: Stephen M Blackburn Kathryn S McKinley Presenter: Jun Tao.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
Runtime System CS 153: Compilers. Runtime System Runtime system: all the stuff that the language implicitly assumes and that is not described in the program.
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.
More Distributed Garbage Collection DC4 Reference Listing Distributed Mark and Sweep Tracing in Groups.
CS333 Intro to Operating Systems Jonathan Walpole.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
Lecture 10 Page 1 CS 111 Summer 2013 File Systems Control Structures A file is a named collection of information Primary roles of file system: – To store.
Memory Management -Memory allocation -Garbage collection.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
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.
Chapter - 2 Data strucuters for Language processing.
Memory Management CSCI 2720 Spring What is memory management? “the prudent utilization of this scarce resource (memory), whether by conservation,
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Garbage Collecting the World Presentation: Mark Mastroieni Authors: Bernard Lang, Christian Queinne, Jose Piquer.
Garbage Collection What is garbage and how can we deal with it?
Core Java Garbage Collection LEVEL – PRACTITIONER.
Jonathan Walpole Computer Science Portland State University
Storage 18-May-18.
Dynamic Memory Allocation
File System Structure How do I organize a disk into a file system?
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Optimizing Malloc and Free
CS Introduction to Operating Systems
Simulated Pointers.
Smart Pointers.
Simulated Pointers.
Strategies for automatic memory management
Presentation made by Steponas Šipaila
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Automating Memory Management
CMPE 152: Compiler Design May 2 Class Meeting
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Garbage Collection CS Introduction to Operating Systems

Freeing Memory Some systems require user to call free when finished with memory –C / C++ –reason for destructors in C++ Other systems detect unused memory and reclaim it –Garbage Collection –this is what Java does

Garbage Collection Basic idea –keep track of what memory is referenced and when it is no longer accessible, reclaim the memory Example –linked list

Example Assume programmer does the following –obj1.next = obj2.next; next Obj1 head tail next Obj1 next Obj1 next Obj1 head tail next Obj1 next Obj1

Example Now there is no way for programmer to reference obj2 –it’s garbage In system without garbage collection this is called a memory leak –location can’t be used but can’t be reallocated –waste of memory and can eventually crash a program In system with garbage collection this chunk will be found and reclaimed

Mark-and-Sweep Basic idea –go through all memory and mark every chunk that is referenced –make a second pass through memory and remove all chunks not marked OS p2 = 650p2 = 360 Mark chunks 0, 1, and 3 as marked Place chunk 2 on the free list (turn it into a hole) 0123

Mark-and-Sweep Issues Have to be able to identify all references –this is difficult in some languages –similar to compaction Requires jumping all over memory –terrible for performance cache hits virtual memory Have to stop everything else to do Search time proportional to non-garbage –may require lots of work for little reward

Reference Counting Basic idea –give each chunk a special field that is the number of references to chunk –whenever a new reference is made, increment field by 1 –whenever a reference is removed, decrement field by 1 –when reference count goes to zero, collect chunk Requires compiler support

Reference Counting Example –everything in italics is added by compiler Object p = new Object; p.count++; Object q = new Object; q.count++; p.count--; if(p.count == 0) collect p p = q; p.count++; p 1 1 q 0 2

Reference Counting Above example does not check for NULL reference Object p = new Object p.count++; p.count--; p = NULL; if(p != NULL) p.count++;

Reference Counting Issues What about pointers inside 0 referenced page? 01 –both of these are garbage –before reclaiming a chunk, must go through all references in the chunk decrement the chunk they reference by 1

Reference Counting Issues What about cycles? p = new Object(); q = new Object(); p[3] = q; q[4] = p; p = q = NULL; 1 p q

Reference Counting Issues Both of the chunks above are garbage Both of the chunks above have a reference count of 1 Neither chunk will be reclaimed Reference counting fails conservatively –it may not collect all the garbage –but it will never throw away non-garbage

Reference Counting Issues Final conclusions –relatively fast it does collection immediately on garbage –safe never throws away non-garbage –mostly works has problems with cycles

Generational Garbage Collection Basic idea –occasionally create a new area in memory –every time an object is referenced, move it to that new area –when the original area becomes sparse, run mark-and-sweep on it move surviving chunks to the new area This method also compacts data in new region

Generational Garbage Collection Example –have initial memory – mem init –after time t create a new area – mem new –chunks r and v are unreferenced at time t mem init pqrstv mem new pqst mem init rv references after time t p.invokeMethod(); s.someField = someValue; q.doSomething(); Run Test-and-Sweep to move chunk v into the new area Then release all of mem init

Generational Garbage Collection After running for awhile, there should be multiple memory areas –some of these areas will consist of long lived chunks do collection on these areas infrequently –some of these areas will consist of short lived chunks do collection on these areas frequently This is where the name of the algorithm comes from

Generational Collection Issues Same issues as those for compaction –identifying pointers –overhead of copying data –must interrupt the system to run the algorithm On the good side –much faster than mark-and-sweep –collects all the garbage unlike reference counting This is the method of choice in today’s world