CMSC 330: Organization of Programming Languages

Similar documents
CMSC 330: Organization of Programming Languages

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

Lecture 13: Garbage Collection

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

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

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

Programming Language Implementation

Garbage Collection. Steven R. Bagley

Automatic Memory Management

Heap storage. Dynamic allocation and stacks are generally incompatible.

Garbage Collection (1)

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

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

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

Heap Management. Heap Allocation

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Run-Time Environments/Garbage Collection

Garbage Collection. Hwansoo Han

G Programming Languages - Fall 2012

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

CA341 - Comparative Programming Languages

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

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Managed runtimes & garbage collection

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

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

Structure of Programming Languages Lecture 10

CS61C : Machine Structures

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

Lecture 15 Garbage Collection

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

Automatic Garbage Collection

Garbage Collection Algorithms. Ganesh Bikshandi

Garbage Collection. Weiyuan Li

Lecture Notes on Garbage Collection

CS 241 Honors Memory

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

Garbage Collection Techniques

Robust Memory Management Schemes

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

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

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.

In Java we have the keyword null, which is the value of an uninitialized reference type

CS61C : Machine Structures

Compiler Construction D7011E

Habanero Extreme Scale Software Research Project

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

Compiler Construction

Class Information ANNOUCEMENTS

Compiler Construction

Garbage Collection. Vyacheslav Egorov

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

Exploiting the Behavior of Generational Garbage Collector

Lecture Notes on Garbage Collection

CS61, Fall 2012 Section 2 Notes

CSC 1600 Memory Layout for Unix Processes"

CS61C : Machine Structures

CS61C : Machine Structures

Lecture 13: Complex Types and Garbage Collection

Dynamic Memory Management

Dynamic Storage Allocation

CMSC 330: Organization of Programming Languages. Ownership, References, and Lifetimes in Rust

Programmer Directed GC for C++ Michael Spertus N2286= April 16, 2007

Concepts Introduced in Chapter 7

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

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Types II. Hwansoo Han

CS558 Programming Languages

Dynamic Memory Management

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Lecture 23: Object Lifetime and Garbage Collection

Dynamic Memory Management! Goals of this Lecture!

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

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

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

Memory Allocation III

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

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Run-time Environments -Part 3

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Quantifying the Performance of Garbage Collection vs. Explicit Memory Management

Memory management has always involved tradeoffs between numerous optimization possibilities: Schemes to manage problem fall into roughly two camps

Reference Counting. Reference counting: a way to know whether a record has other users

CSC 533: Organization of Programming Languages. Spring 2005

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

Algorithms for Dynamic Memory Management (236780) Lecture 1

C11: Garbage Collection and Constructors

CS Computer Systems. Lecture 8: Free Memory Management

Advanced Programming & C++ Language

CS558 Programming Languages

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

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

The Generational Hypothesis

Implementation Garbage Collection

Limitations of the stack

HOT-Compilation: Garbage Collection

CS61C : Machine Structures

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CS558 Programming Languages

o Code, executable, and process o Main memory vs. virtual memory

Transcription:

CMSC 330: Organization of Programming Languages Memory Management and Garbage Collection CMSC 330 - Spring 2013 1

Memory Attributes! Memory to store data in programming languages has the following lifecycle Allocation When the memory is allocated to the program Persistence (lifetime) How long allocated memory is used by the program Recovery When the system recovers the memory for reuse! The allocator is the system feature that performs allocation and recovery CMSC 330 - Spring 2013 2

Memory Attributes (cont.)! Most programming languages are concerned with some subset of the following 4 memory classes 1. Static (or fixed) memory 2. Automatic memory 3. Dynamically allocated memory 4. Persistent memory CMSC 330 - Spring 2013 3

Memory Classes! Static memory Usually at a fixed address Persistence The execution of program Allocation For entire execution Recovery By system when program terminates Allocator The compiler! Automatic memory Usually on a stack Persistence Lifetime of method using that data Allocation When method is invoked Recovery When method terminates Allocator Typically by the compiler, sometimes programmer CMSC 330 - Spring 2013 4

Memory Classes (cont.)! Dynamic memory Addresses allocated on demand in an area called the heap Persistence As long as memory is needed Allocation Explicitly by programmer, or implicitly by the compiler Recovery Either by programmer or automatically (when possible and depends upon language) Allocator Manages the free/available space in the heap CMSC 330 - Spring 2013 5

Memory Classes (cont.)! Persistent memory Usually the file system Persistence Multiple executions of a program E.g., files or databases Allocation By program or user Often outside of program execution Recovery When data no longer needed Dealing with persistent memory databases CMSC 424 CMSC 330 - Spring 2013 6

Memory Management in C! Local variables live on the stack Allocated at function invocation time Deallocated when function returns Storage space reused after function returns! Space on the heap allocated with malloc() Must be explicitly freed with free() Called explicit or manual memory management Deletions must be done by the user CMSC 330 - Spring 2013 7

Memory Management Mistakes! May forget to free memory (memory leak) { int *x = (int *) malloc(sizeof(int)); }! May retain ptr to freed memory (dangling pointer) { int *x =...malloc(); } free(x); *x = 5; /* oops! */! May try to free something twice { int *x =...malloc(); free(x); free(x); } This may corrupt the memory management data structures E.g., the memory allocator maintains a free list of space on the heap that s available CMSC 330 - Spring 2013 8

Fragmentation! Another memory management problem! Example sequence of calls allocate(a); allocate(x); allocate(y); free(a); allocate(z); free(y); allocate(b); Not enough contiguous space for b CMSC 330 - Spring 2013 9

Ways to Avoid Mistakes in C! Don t allocate memory on the heap Could lead to confusing code! Never free memory OS will reclaim process s memory anyway at exit Memory is cheap; who cares about a little leak?! But: Both of the above two may be impractical! Can avoid all three problems by using automatic memory management Though it does not prevent all leaks, as we will see CMSC 330 - Spring 2013 10

Automatic memory management! Primary goal: automatically reclaim dynamic memory Secondary goal: also avoid fragmentation! Insight: You can do reclamation and memory compaction if you can identify every pointer in a program You can move the allocated storage, then redirect pointers to it Compiler ensures perfect knowledge LISP, OCAML, Java, Prolog (with caveats), but not in C, C++, Pascal, Ada CMSC 330 - Spring 2013 11

Strategy! At any point during execution, can divide the objects in the heap into two classes Live objects will be used later Dead objects will never be used again They are garbage! Thus we need algorithms that can 1. Distinguish live from dead objects 2. Reclaim the dead objects and retain the live ones CMSC 330 - Spring 2013 12

Determining liveness! In most languages we can t know for sure which objects are really live or dead Undecidable, like solving the halting problem! Thus we need to make an approximation OK if we decide something is live when it s not But we d better not deallocate an object that will be used later on CMSC 330 - Spring 2013 13

Liveness by Reachability! An object is reachable if it can be accessed by chasing pointers from live data! Safe policy: delete unreachable objects An unreachable object can never be accessed again by the program The object is definitely garbage A reachable object may be accessed in the future The object could be garbage but will be retained anyway Could lead to memory leaks CMSC 330 - Spring 2013 14

Roots! At a given program point, we define liveness as being data reachable from the root set Global variables What are these in Java? Ruby? OCaml? Local variables of all live method activations I.e., the stack! At the machine level Also consider the register set Usually stores local or global variables! Next Techniques for pointer chasing CMSC 330 - Spring 2013 15

Reference Counting! Idea: Each object has count of number of pointers to it from the roots or other objects When count reaches 0, object is unreachable! Count tracking code may be manual or automatic! In regular use C++ (smart pointer library), Cocoa (manual), Python! Method doesn t address fragmentation problem! Invented by Collins in 1960 A method for overlapping and erasure of lists. Communications of the ACM, December 1960 CMSC 330 - Spring 2013 16

Reference Counting Example stack 1 2 1 CMSC 330 - Spring 2013 17

Reference Counting Example (cont.) stack 1 2 1 1 1 CMSC 330 - Spring 2013 18

Reference Counting Example (cont.) stack 1 2 1 1 1 CMSC 330 - Spring 2013 19

Reference Counting Example (cont.) stack 1 2 1 1 0 1 CMSC 330 - Spring 2013 20

Reference Counting Example (cont.) stack 1 2 1 1 CMSC 330 - Spring 2013 21

Reference Counting Example (cont.) stack 1 2 1 0 1 CMSC 330 - Spring 2013 22

Reference Counting Example (cont.) stack 1 CMSC 330 - Spring 2013 23

Reference Counting Tradeoffs! Advantage Incremental technique Generally small, constant amount of work per memory write With more effort, can even bound running time! Disadvantages Cascading decrements can be expensive Requires extra storage for reference counts Need other means to collect cycles, for which counts never go to 0 CMSC 330 - Spring 2013 24

Tracing Garbage Collection! Idea: Determine reachability as needed, rather than by stored counts! Every so often, stop the world and Deem all objects on stack as live Follow pointers from live objects to expand the live object set Repeat until no more reachable objects Deallocate any non-reachable objects! Two main variants of tracing GC Mark/sweep (McCarthy 1960) and stop-and-copy (Cheney 1970) CMSC 330 - Spring 2013 25

Mark and Sweep GC! Two phases Mark phase: trace the heap and mark all reachable objects Sweep phase: go through the entire heap and reclaim all unmarked objects CMSC 330 - Spring 2013 26

Mark and Sweep Example stack CMSC 330 - Spring 2013 27

Mark and Sweep Example (cont.) stack CMSC 330 - Spring 2013 28

Mark and Sweep Example (cont.) stack CMSC 330 - Spring 2013 29

Mark and Sweep Example (cont.) stack CMSC 330 - Spring 2013 30

Mark and Sweep Example (cont.) stack CMSC 330 - Spring 2013 31

Mark and Sweep Example (cont.) stack CMSC 330 - Spring 2013 32

Mark and Sweep Example (cont.) stack CMSC 330 - Spring 2013 33

Mark and Sweep Advantages! No problem with cycles! Memory writes have no cost! Non-moving Live objects stay where they are Makes conservative GC possible Used when identification of pointer vs. int uncertain CMSC 330 - Spring 2013 34

Mark and Sweep Disadvantages! Fragmentation Available space broken up into many small pieces Thus many mark-and-sweep systems may also have a compaction phase (like defragmenting your disk)! Cost proportional to heap size Sweep phase needs to traverse whole heap it touches dead memory to put it back on to the free list! Stop-the-world variant not real-time Bad if your car s braking system performs GC while you are trying to stop at a busy intersection Can make the marking/sweeping concurrent/incremental CMSC 330 - Spring 2013 35

Stop and Copy GC! Like mark and sweep, but only touches live objects Divide heap into two equal parts (semispaces) Only one semispace active at a time At GC time, flip semispaces 1. Trace the live data starting from the roots 2. Copy live data into other semispace 3. Declare everything in current semispace dead 4. Switch to other semispace CMSC 330 - Spring 2013 36

Stop and Copy Example stack CMSC 330 - Spring 2013 37

Stop and Copy Example (cont.) stack CMSC 330 - Spring 2013 38

Stop and Copy Example (cont.) stack CMSC 330 - Spring 2013 39

Stop and Copy Example (cont.) stack CMSC 330 - Spring 2013 40

Stop and Copy Tradeoffs! Advantages Only touches live data No fragmentation (automatically compacts) Will probably increase locality! Disadvantages Requires twice the memory space Like mark and sweep, need to stop the world Program must stop running to let garbage collector move around data in the heap! Idea: We reduce collection pause time by not traversing the whole heap at once CMSC 330 - Spring 2013 41

The Generational Principle More objects live Young objects die quickly; old objects keep living Object lifetime increases CMSC 330 - Spring 2013 42

Generational Collection! Long lived objects visited multiple times Idea: Have more than one heap region, divide into generations Older generations collected less often Objects that survive many collections get promoted into older generations Need to track pointers from old to young generations to use as roots for young generation collection Tracking one in the remembered set! One popular setup: Generational stop and copy CMSC 330 - Spring 2013 43

Conservative Garbage Collection (for C)! For C, we cannot be sure which elements of an object are pointers Because of incomplete type information, the use of unsafe casts, etc.! Idea: suppose it is a pointer if it looks like one Most pointers are within a certain address range, they are word aligned, etc. May retain memory spuriously! Different styles of conservative collector Mark-sweep: important that objects not moved Mostly-copying: can move objects you are sure of CMSC 330 - Spring 2013 44

Memory Management in Ruby! Local variables live on the stack Storage reclaimed when method returns! Objects live on the heap Created with calls to Class.new! Objects never explicitly freed Ruby uses automatic memory management Uses a garbage collector to reclaim memory CMSC 330 - Spring 2013 45

Memory Management in OCaml! Local variables live on the stack! Tuples, closures, and constructed types live on the heap let x = (3, 4) (* heap-allocated *) let f x y = x + y in f 3 (* result heap-allocated *) type a t = None Some of a None (* not on the heap just a primitive *) Some 37 (* heap-allocated *)! Garbage collection reclaims memory CMSC 330 - Spring 2013 46

Memory Management in Java! Local variables live on the stack Allocated at method invocation time Deallocated when method returns! Other data lives on the heap Memory is allocated with new But never explicitly deallocated Java uses automatic memory management CMSC 330 - Spring 2013 47

Java HotSpot SDK 1.4.2 Collector! Multi-generational, hybrid collector Young generation Stop and copy collector Tenured generation Mark and sweep collector Permanent generation No collection! Questions Why does using a copy collector for the youngest generation make sense? What apps will be penalized by this setup? CMSC 330 - Spring 2013 48

More Issues in GC (cont.)! Stopping the world is a big hit Unpredictable performance Bad for real-time systems Need to stop all threads Without a much more sophisticated GC! One-size-fits-all solution Sometimes, GC just gets in the way But correctness comes first CMSC 330 - Spring 2013 49

What Does GC Mean to You?! Ideally, nothing GC should make programming easier GC should not affect performance (much)! Usually bad idea to manage memory yourself Using object pools, free lists, object recycling, etc GC implementations have been heavily tuned May be more efficient than explicit deallocation! If GC becomes a problem, hard to solve You can set parameters of the GC You can modify your program CMSC 330 - Spring 2013 50

Increasing Memory Performance! Don t allocate as much memory Less work for your application Less work for the garbage collector! Don t hold on to references Null out pointers in data structures Example Object a = new Object; use a a = null; // when a is no longer needed CMSC 330 - Spring 2013 51

Find the Memory Leak class Stack { private Object[] stack; private int index; public Stack(int size) { stack = new Object[size]; } public void push(object o) { stack[index++] = o; } public void pop() { stack[index] = null; // null out ptr return stack[index--]; } } From Haggar, Garbage Collection and the Java Platform Memory Model Answer: pop() leaves item on stack array; storage not reclaimed CMSC 330 - Spring 2013 52