Garbage collection (& Midterm Topics) David Walker COS 320.

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

Part IV: Memory Management
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Automatic Memory Management Noam Rinetzky Schreiber 123A /seminar/seminar1415a.html.
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 Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.
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++)
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
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.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Generational Stack Collection And Profile driven Pretenuring Perry Cheng Robert Harper Peter Lee Presented By Moti Alperovitch
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Age-Oriented Concurrent Garbage Collection Harel Paz, Erez Petrank – Technion, Israel Steve Blackburn – ANU, Australia April 05 Compiler Construction Scotland.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Garbage collection (& Midterm Topics) David Walker COS 320.
Linked lists and memory allocation Prof. Noah Snavely CS1114
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 9 – Real Memory Organization and Management Outline 9.1 Introduction 9.2Memory Organization.
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.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
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.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
1 Memory Management Basics. 2 Program P Basic Memory Management Concepts Address spaces Physical address space — The address space supported by the hardware.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
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.
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.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Chapter 7 Memory Management Eighth Edition William Stallings Operating Systems: Internals and Design Principles.
Memory Management Chapter 5 Advanced Operating System.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Garbage Collection What is garbage and how can we deal with it?
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Chapter 9 – Real Memory Organization and Management
Concepts of programming languages
Automatic Memory Management/GC
Automatic Memory Management
Smart Pointers.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Strategies for automatic memory management
Memory Management Kathryn McKinley.
Closure Representations in Higher-Order Programming Languages
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Automating Memory Management
Garbage Collection What is garbage and how can we deal with it?
Mooly Sagiv html:// Garbage Collection Mooly Sagiv html://
Presentation transcript:

Garbage collection (& Midterm Topics) David Walker COS 320

Mid-term Topics Midterm is in class Tuesday March 22 nd –be on time Overall, you may be asked questions on –anything discussed in class before today (thursday march 10) –anything you did on assignments 1-4 –anything in the corresponding chapters in the textbooks (Appel) and online notes (Harper) at the end of each chapter in Appel there are a series of questions and exercises – midterm questions may be similar. I suggest practicing for the midterm by doing some of these questions. –a full range of topics appears on the course “schedule” web page I will be away next week sunday-thursday –schedule an appointment Friday or Monday if you need help. –schedule an appointment with Guilherme (he will also be away)

Topics: compiler & interpreter structure what are the main phases in a compiler? –lexing, parsing, type checking, back-end how do you implement an interpreter? assignment #1

Topics: Lexing What is a lexer? Regular expressions How do you use ML-lex Appel Chap 2, 2.1, 2.2, 2.5 –not relationships between REs and DFAs or NDFAs

Topics: Parsing context-free grammars & derivations bottom-up parsing; top-down parsing different kinds of grammars: ambiguous, LL(k), LR(k), SLR, LALR ML-Yacc semantic actions & generating abstract syntax assignment #3 Appel Chapters 3, 4

Topics: Type Checking Typing rules & typing derivations MinML types & Fun types Subtyping Type checking algorithms Type inference algorithms Assignment #4 Harper chapter 9, 26 (not lemmas or proofs)

The back end! Today & next time: garbage collection

GC Every modern programming language allows programmers to allocate new storage dynamically –New records, arrays, tuples, objects, closures, etc. Every modern language needs facilities for reclaiming and recycling the storage used by programs It’s usually the most complex aspect of the run- time system for any modern language (Java, ML, Lisp, Scheme, Modula, …)

Memory layout static data stack heap grows to preset limit new pages allocated via calls to OS physical memory TLB address translation per process virtual memory

GC What is garbage? –A value is garbage if it will not be used in any subsequent computation by the program Is it easy to determine which objects are garbage?

GC What is garbage? –A value is garbage if it will not be used in any subsequent computation by the program Is it easy to determine which objects are garbage? –No. It’s undecidable. Eg: if long-and-tricky-computation then use v else don’t use v

GC Since determining which objects are garbage is tricky, people have come up with many different techniques –It’s the programmers problem: Explicit allocation/deallocation –Reference counting –Tracing garbage collection Mark-sweep, copying collection Generational GC

Explicit MM User library manages memory; programmer decides when and where to allocate and deallocate –void* malloc(long n) –void free(void *addr) –Library calls OS for more pages when necessary –Advantage: people are smart –Disadvantage: people are dumb and they really don’t want to bother with such details if they can avoid it

Explicit MM How does malloc/free work? –Blocks of unused memory stored on a freelist –malloc: search free list for usable memory block –free: put block onto the head of the freelist freelist

Explicit MM Drawbacks –malloc is not free: we might have to do a significant search to find a big enough block –As program runs, the heap fragments leaving many small, unusable pieces freelist

Explicit MM Solutions: –Use multiple free lists, one for each block size Malloc and free become O(1) But can run out of size 4 blocks, even though there are many size 6 blocks or size 2 blocks! –Blocks are powers of 2 Subdivide blocks to get the right size Adjacent free blocks merged into the next biggest size still possibly 30% wasted space –Crucial point: there is no magic bullet. Memory management always has a cost. We want to minimize costs and, these days, maximize reliability.

Automatic MM Languages with explicit MM are much harder to program than languages with automatic MM –Always worrying about dangling pointers, memory leaks: a huge software engineering burden –Impossible to develop a secure system, impossible to use these languages in emerging applications involving mobile code –Soon, languages with unsafe, explicit MM will all but disappear eg: Microsoft is pouring $$$ into developing safe language technology, including a new research project on dependable operating system construction

Automatic MM Question: how do we decide which objects are garbage? –Can’t do it exactly –Therefore, We conservatively approximate –Normal solution: an object is garbage when it becomes unreachable from the roots The roots = registers, stack, global static data If there is no path from the roots to an object, it cannot be used later in the computation so we can safely recycle its memory

Object Graph –How should we test reachability? r1 stack r2

Object Graph –How should we test reachability? r1 stack r2

Reference Counting Keep track of the number of pointers to each object (the reference count). When the reference count goes to 0, the object is unreachable garbage

Object Graph –Reference counting can’t detect cycles r1 stack r2

Reference Counting –In place of a single assignment x.f = p: z = x.f c = z.count c = c – 1 z.count = c If c = 0 call putOnFreeList(z) x.f = p c = p.count c = c + 1 p.count = c -Ouch, that hurts performace-wise! -Dataflow analysis can eliminate some increments and decrements, but many remain -Reference counting used in some special cases but not usually as the primary GC mechanism in a language implementation

Copying Collection Basic idea: use 2 heaps –One used by program –The other unused until GC time GC: –Start at the roots & traverse the reachable data –Copy reachable data from the active heap (from- space) to the other heap (to-space) –Dead objects are left behind in from space –Heaps switch roles

Copying Collection to-spacefrom-space roots

Copying GC Cheny’s algorithm for copying collection –Traverse data breadth first, copying objects from from-space to to-space root scan next

Copying GC Cheny’s algorithm for copying collection –Traverse data breadth first, copying objects from from-space to to-space root scan next

Copying GC Cheny’s algorithm for copying collection –Traverse data breadth first, copying objects from from-space to to-space root scan next

Copying GC Cheny’s algorithm for copying collection –Traverse data breadth first, copying objects from from-space to to-space root scan next

Copying GC Cheny’s algorithm for copying collection –Traverse data breadth first, copying objects from from-space to to-space root scan next

Copying GC Cheny’s algorithm for copying collection –Traverse data breadth first, copying objects from from-space to to-space root scan next

Copying GC Cheny’s algorithm for copying collection –Traverse data breadth first, copying objects from from-space to to-space root scan next Done when next = scan

Copying GC Cheny’s algorithm for copying collection –Traverse data breadth first, copying objects from from-space to to-space root scan next Done when next = scan

Copying GC Pros –Simple & collects cycles –Run-time proportional to # live objects –Automatic compaction eliminates fragmentation –Fast allocation: pointer increment by object size Cons –Precise type information required (pointer or not) Tag bits take extra space; normally use header word –Twice as much memory used as program requires Usually, we anticipate live data will only be a small fragment of store Allocate until 70% full From-space = 70% heap; to-space = 30% –Long GC pauses = bad for interactive, real-time apps

Baker’s Concurrent GC GC pauses avoided by doing GC incrementally Program only holds pointers to to-space On field fetch, if pointer to from-space, copy object and fix pointer –Extra fetch code = 20% performance penalty –But no long pauses ==> better response time On swap, copy roots as before

Generational GC Empirical observation: if an object has been reachable for a long time, it is likely to remain so Empirical observation: in many languages (especially functional languages), most objects died young Conclusion: we save work by scanning the young objects frequently and the old objects infrequently

Generational GC Assign objects to different generations G0, G1,… –G0 contains young objects, most likely to be garbage –G0 scanned more often than G1 –Common case is two generations (new, tenured) –Roots for GC of G0 include all objects in G1 in addition to stack, registers

Generational GC How do we avoid scanning tenured objects? –Observation: old objects rarely point to new objects Normally, object is created and when it initialized it will point to older objects, not newer ones Only happens if old object modified well after it is created In functional languages that use mutation infrequently, pointers from old to new are very uncommon –Compiler inserts extra code on object field pointer write to catch modifications to old objects –Remembered set is used to keep track of objects that point into younger generation. Remembered set included in set of roots for scanning.

Generational GC Other issues –When do we promote objects from young generation to old generation Usually after an object survives a collection, it will be promoted –How big should the generations be? Appel says each should be exponentially larger than the last –When do we collect the old generation? After several minor collections, we do a major collection

Generational GC Other issues –Sometimes different GC algorithms are used for the new and older generations. Why? Because the have different characteristics –Copying collection for the new Less than 10% of the new data is usually live Copying collection cost is proportional to the live data –Mark-sweep for the old We’ll get to this in the next class!