Lecture Outine. Why Automatic Memory Management? Garbage Collection. Automatic Memory Management. Three Techniques. Lecture 18

Similar documents
Automatic Memory Management

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there.

Automatic Memory Management

Automatic Memory Management

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Run-Time Environments/Garbage Collection

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Heap Management. Heap Allocation

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Lecture 13: Garbage Collection

Lecture 15 Garbage Collection

CS61C : Machine Structures

CMSC 330: Organization of Programming Languages

Lecture 15 Advanced Garbage Collection

CMSC 330: Organization of Programming Languages

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Robust Memory Management Schemes

Automatic Garbage Collection

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Run-time Environments -Part 3

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Lecture Notes on Advanced Garbage Collection

Lecture Notes on Garbage Collection

Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

Garbage Collection. Steven R. Bagley

Garbage Collection (1)

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

G Programming Languages - Fall 2012

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Compiler Construction

CS2210: Compiler Construction. Runtime Environment

Programming Language Implementation

CS 4120 Lecture 37 Memory Management 28 November 2011 Lecturer: Andrew Myers

Compiler Construction

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Implementation Garbage Collection

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

Dynamic Storage Allocation

CS2210: Compiler Construction. Runtime Environment

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Programming Languages

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

CS61C : Machine Structures

Lecture Notes on Garbage Collection

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Algorithms for Dynamic Memory Management (236780) Lecture 1

Garbage Collection Algorithms. Ganesh Bikshandi

Dynamic Memory Management! Goals of this Lecture!

Garbage Collection. Hwansoo Han

Dynamic Memory Allocation: Advanced Concepts

Habanero Extreme Scale Software Research Project

CS S-11 Memory Management 1

Lecture Notes on Memory Layout

Garbage Collection (2) Advanced Operating Systems Lecture 9

Algorithms for Dynamic Memory Management (236780) Lecture 4. Lecturer: Erez Petrank

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Dynamic Memory Management

Lecture 7: Binding Time and Storage

HOT-Compilation: Garbage Collection

Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

Garbage Collection Techniques

Lecture 13: Complex Types and Garbage Collection

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

FS Consistency & Journaling

(notes modified from Noah Snavely, Spring 2009) Memory allocation

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

Compiler Construction D7011E

Mark-Sweep and Mark-Compact GC

Garbage Collection. CS 351: Systems Programming Michael Saelee

Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

Performance of Non-Moving Garbage Collectors. Hans-J. Boehm HP Labs

Qualifying Exam in Programming Languages and Compilers

Chapter 8 :: Composite Types

CS61C Midterm Review on C & Memory Management

Garbage Collection in Hash-Addressing List Processors. CGTM #35 January 2, 1968 J.R. Ehrman

CS 241 Honors Memory

Memory Management. Before We Begin. Process s Memory Address Space. Process Memory. CSE 120: Principles of Operating Systems.

! What is virtual memory and when is it useful? ! What is demand paging? ! What pages should be. ! What is the working set model?

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

Exploiting the Behavior of Generational Garbage Collector

2. Reachability in garbage collection is just an approximation of garbage.

High-Level Language VMs

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Proposal to Acknowledge that Garbage Collection for C++ is Possible X3J16/ WG21/N0932. Bjarne Stroustrup. AT&T Research.

Dynamic Memory Allocation

Opera&ng Systems CMPSCI 377 Garbage Collec&on. Emery Berger and Mark Corner University of Massachuse9s Amherst

Garbage Collection. Lecture Compilers SS Dr.-Ing. Ina Schaefer. Software Technology Group TU Kaiserslautern. Ina Schaefer Garbage Collection 1

CS558 Programming Languages

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Book keeping. Will post HW5 tonight. OK to work in pairs. Midterm review next Wednesday

Lectures 13 & 14. memory management

Segmentation with Paging. Review. Segmentation with Page (MULTICS) Segmentation with Page (MULTICS) Segmentation with Page (MULTICS)

Transcription:

Lecture Outine Why utomatic Memory Management? utomatic Memory Management Lecture 18 Garbage ollection Three Techniques Mark and Sweep Stop and opy Reference ounting Prof. Bodik S 164 Lecture 18 1 Prof. Bodik S 164 Lecture 18 2 Why utomatic Memory Management? Type Safety and Memory Management Storage management is still a hard problem in modern programming and ++ programs have many storage bugs forgetting to free unused memory dereferencing a dangling pointer overwriting parts of a data structure by accident and so on... Storage bugs are hard to find a bug can lead to a visible effect far away in time and program text from the source Prof. Bodik S 164 Lecture 18 3 Some storage bugs can be prevented in a strongly typed language e.g., you cannot overrun the array limits an types prevent errors in programs with manual ation and deation of memory? Some fancy type systems (linear types) were designed for this purpose but they complicate programming significantly If you want type safety then you must use automatic memory management Prof. Bodik S 164 Lecture 18 4 utomatic Memory Management This is an old problem: Studied since the 1950s for LISP There are several well-known techniques for performing completely automatic memory management Until recently they were unpopular outside the Lisp family of languages just like type safety used to be unpopular The Basic Idea When an object that takes memory space is created, unused space is automatically ated In Decaf, new objects are created by new X fter a while there is no more unused space Some space is occupied by objects that will never be used again This space can be freed to be reused later Prof. Bodik S 164 Lecture 18 5 Prof. Bodik S 164 Lecture 18 6

The Basic Idea (ont.) How can we tell whether an object will never be used again? In general it is impossible to tell We will have to use a heuristic to find many (not all) objects that will never be used again Observation: a program can use only the objects that it can find: x = new (); x = y; fter x = y there is no way to access the newly ated object Garbage n object x is reachable if and only if: register contains a pointer to x, or nother reachable object y contains a pointer to x You can find all reachable objects by ing from registers and following all the pointers n unreachable object can never by referred by the program These objects are called garbage Prof. Bodik S 164 Lecture 18 7 Prof. Bodik S 164 Lecture 18 8 Reachability is an pproximation Tracing Reachable Values in Decaf onsider the program: x = new (); y = new B(); x = y; if alwaystrue() { x = new () } else { x.foo(); } fter x = y (assuming y becomes dead there) The object is not reachable anymore The object B is reachable (through x) Thus B is not garbage and is not collected But object B is never going to be used Prof. Bodik S 164 Lecture 18 9 In a simple implementation of Decaf, the only register is the accumulator it points to an object and this object may point to other objects, etc. The stack is more complex each stack frame contains pointers e.g., method parameters each stack frame also contains non-pointers e.g., return address if we know the layout of the frame we can find the pointers in it Prof. Bodik S 164 Lecture 18 10 Simple Example acc SP B Frame 1 Frame 2 We tracing from acc and stack they are called the s Note that B and D are not reachable from acc or the stack Thus we can reuse their storage D Prof. Bodik S 164 Lecture 18 11 E Elements of Garbage ollection Every garbage collection scheme has the following steps 1. llocate space as needed for new objects 2. When space runs out: a) ompute what objects might be used again (generally by tracing objects reachable from a set of registers) b) Free the space used by objects not found in (a) Some strategies perform garbage collection before the space actually runs out Prof. Bodik S 164 Lecture 18 12

Mark and Sweep Mark and Sweep Example When memory runs out, G executes two phases the mark phase: traces reachable objects the sweep phase: collects garbage objects Every object has an extra bit: the mark bit reserved for memory management initially the mark bit is 0 set to 1 for the reachable objects in the mark phase Prof. Bodik S 164 Lecture 18 13 0 B 0 0 D 0 E 0 F 0 free fter mark: 1 B 0 1 D 0 E 0 F 1 fter sweep: free 0 B 0 0 D 0 E 0 F 0 free Prof. Bodik S 164 Lecture 18 14 The Mark Phase let todo = { all s } while todo do pick v todo todo todo - { v } if mark(v) = 0 then (* v is unmarked yet *) mark(v) 1 let v 1,...,v n be the pointers contained in v todo todo {v 1,...,v n } fi od The Sweep Phase The sweep phase s the heap looking for objects with mark bit 0 these objects have not been visited in the mark phase they are garbage ny such object is added to the free list The objects with a mark bit 1 have their mark bit reset to 0 Prof. Bodik S 164 Lecture 18 15 Prof. Bodik S 164 Lecture 18 16 The Sweep Phase (ont.) /* sizeof(p) is the size of block ing at p */ p bottom of heap while p < top of heap do if mark(p) = 1 then mark(p) 0 else add block p...(p+sizeof(p)-1) to freelist fi p p + sizeof(p) od Details While conceptually simple, this algorithm has a number of tricky details this is typical of G algorithms serious problem with the mark phase it is invoked when we are out of space yet it needs space to construct the todo list the size of the todo list is unbounded so we cannot reserve space for it a priori Prof. Bodik S 164 Lecture 18 17 Prof. Bodik S 164 Lecture 18 18

Mark and Sweep: Details The todo list is used as an auxiliary data structure to perform the reachability analysis There is a trick that allows the auxiliary data to be stored in the objects themselves pointer reversal: when a pointer is followed it is reversed to point to its parent Similarly, the free list is stored in the free objects themselves Prof. Bodik S 164 Lecture 18 19 Mark and Sweep. Evaluation Space for a new object is ated from the new list a block large enough is picked an area of the necessary size is ated from it the left-over is put back in the free list Mark and sweep can fragment the memory dvantage: objects are not moved during G no need to update the pointers to objects works for languages like and ++ Prof. Bodik S 164 Lecture 18 20 nother Technique: Stop and opy Stop and opy Garbage ollection Memory is organized into two areas Old space: used for ation New space: used as a reserve for G heap pointer old space The heap pointer points to the next free word in the old space llocation just advances the heap pointer Prof. Bodik S 164 Lecture 18 21 Starts when the old space is full opies all reachable objects from old space into garbage is left behind after the copy phase the uses less space than the old one before the collection fter the copy the roles of the old and new spaces are reversed and the program resumes Prof. Bodik S 164 Lecture 18 22 Stop and opy Garbage ollection. Example Implementation of Stop and opy Before collection: B D E F fter collection: heap pointer F free We need to find all the reachable objects, as for mark and sweep s we find a reachable object we copy it into the nd we have to fix LL pointers pointing to it! s we copy an object we store in the old copy a forwarding pointer to the new copy when we later reach an object with a forwarding pointer we know it was already copied Prof. Bodik S 164 Lecture 18 23 Prof. Bodik S 164 Lecture 18 24

Implementation of Stop and opy (ont.) Stop and opy. Example (1) We still have the issue of how to implement the traversal without using extra space The following trick solves the problem: partition the in three contiguous regions copied and ned copied objects whose pointer fields were followed and fixed copied copied objects whose pointer fields were NOT followed empty Prof. Bodik S 164 Lecture 18 25 Before garbage collection B D E F Prof. Bodik S 164 Lecture 18 26 Stop and opy. Example (3) Step 1: opy the objects pointed by s and set forwarding pointers (dotted arrow) Stop and opy. Example (3) Step 2: Follow the pointer in the next unned object () copy the pointed objects (just in this case) fix the pointer in set forwarding pointer B D E F B D E F Prof. Bodik S 164 Lecture 18 27 Prof. Bodik S 164 Lecture 18 28 Stop and opy. Example (4) Follow the pointer in the next unned object () copy the pointed objects (F in this case) Stop and opy. Example (5) Follow the pointer in the next unned object (F) the pointed object () was already copied. Set the pointer same as the forwading pointer B D E F F B D E F F Prof. Bodik S 164 Lecture 18 29 Prof. Bodik S 164 Lecture 18 30

Stop and opy. Example (6) The Stop and opy lgorithm Since caught up with we are done Swap the role of the spaces and resume the program Prof. Bodik S 164 Lecture 18 31 F while <> do let O be the object at pointer for each pointer p contained in O do find O that p points to if O is without a forwarding pointer copy O to (update pointer) set 1st word of old O to point to the new copy change p to point to the new copy of O else set p in O equal to the forwarding pointer fi end for increment pointer to the next object od Prof. Bodik S 164 Lecture 18 32 Stop and opy. Details. s with mark and sweep, we must be able to tell how large is an object when we it nd we must also know where are the pointers inside the object We must also copy any objects pointed to by the stack and update pointers in the stack This can be an expensive operation Stop and opy. Evaluation Stop and copy is generally believed to be the fastest G technique llocation is very cheap Just increment the heap pointer ollection is relatively cheap Especially if there is a lot of garbage Only touch reachable objects But some languages do not allow copying (, ++) Prof. Bodik S 164 Lecture 18 33 Prof. Bodik S 164 Lecture 18 34 Why Doesn t llow opying? onservative Garbage ollection Garbage collection relies on being able to find all reachable objects nd it needs to find all pointers in an object In or ++ it is impossible to identify the contents of objects in memory E.g., how can you tell that a sequence of two memory words is a list cell (with data and next fields) or a binary tree node (with a left and right fields)? Thus we cannot tell where all the pointers are Prof. Bodik S 164 Lecture 18 35 But it is Ok to be conservative: If a memory word looks like a pointer it is considered a pointer it must be aligned it must point to a valid address in the data segment ll such pointers are followed and we overestimate the reachable objects But we still cannot move objects because we cannot update pointers to them What if what we thought to be a pointer is actually an account number? Prof. Bodik S 164 Lecture 18 36

Reference ounting Rather that wait for memory to be exhausted, try to collect an object when there are no more pointers to it Store in each object the number of pointers to that object This is the reference count Each assignment operation has to manipulate the reference count Implementation of Reference ounting new returns an object with a reference count of 1 If x points to an object then let rc(x) refer to the object s reference count Every assignment x = y must be changed: rc(y) = rc(y) + 1 rc(x) = rc(x) - 1 if(rc(x) == 0) then mark x as free x = y Prof. Bodik S 164 Lecture 18 37 Prof. Bodik S 164 Lecture 18 38 Reference ounting. Evaluation dvantages: Easy to implement ollects garbage incrementally without large pauses in the execution Disadvantages: Manipulating reference counts at each assignment is very slow annot collect circular structures Garbage ollection. Evaluation utomatic memory management avoids some serious storage bugs But it takes away control from the programmer e.g., layout of data in memory e.g., when is memory deated Most garbage collection implementation stop the execution during collection not acceptable in real-time applications Prof. Bodik S 164 Lecture 18 39 Prof. Bodik S 164 Lecture 18 40 Garbage ollection. Evaluation Garbage collection is going to be around for a while Researchers are working on advanced garbage collection algorithms: oncurrent: allow the program to run while the collection is happening Generational: do not long-lived objects at every collection Parallel: several collectors working in parallel Prof. Bodik S 164 Lecture 18 41