Algorithms for Dynamic Memory Management (236780) Lecture 1

Similar documents
Automatic Memory Management

CMSC 330: Organization of Programming Languages

THEORY OF COMPILATION

Lecture 13: Garbage Collection

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

CMSC 330: Organization of Programming Languages

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

Run-Time Environments/Garbage Collection

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

How do we mark reachable objects?

Mark-Sweep and Mark-Compact GC

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

Garbage Collection. Steven R. Bagley

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

Heap Management. Heap Allocation

Deallocation Mechanisms. User-controlled Deallocation. Automatic 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.

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

Run-time Environments -Part 3

G Programming Languages - Fall 2012

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

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

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

Implementation Garbage Collection

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

Automatic Garbage Collection

Programming Language Implementation

CS61C : Machine Structures

Lecture Notes on Garbage Collection

Garbage Collection (1)

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

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

CS61C : Machine Structures

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

CS842: Automatic Memory Management and Garbage Collection. Mark and sweep

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

Memory Allocation III

Robust Memory Management Schemes

Garbage Collection Algorithms. Ganesh Bikshandi

CS61C : Machine Structures

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

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

CS 241 Honors Memory

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

CS61C : Machine Structures

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

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Garbage Collection Techniques

Managed runtimes & garbage collection

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

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

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

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

Exploiting the Behavior of Generational Garbage Collector

Dynamic Storage Allocation

Lecture 15 Garbage Collection

Compiler Construction

Dynamic Memory Allocation II October 22, 2008

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

Compiler Construction

Memory Allocation III

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

Garbage Collection. Weiyuan Li

Heap Compression for Memory-Constrained Java

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

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

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Garbage Collection. Hwansoo Han

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

Quantifying the Performance of Garbage Collection vs. Explicit Memory Management

Name, Scope, and Binding. Outline [1]

Memory Allocation III

Memory Allocation III CSE 351 Spring (original source unknown)

Parallel GC. (Chapter 14) Eleanor Ainy December 16 th 2014

Habanero Extreme Scale Software Research Project

Automatic Memory Management

A new Mono GC. Paolo Molaro October 25, 2006

06 A Low-Level Virtual Machine

CS2210: Compiler Construction. Runtime Environment

Lecture 13: Complex Types and Garbage Collection

CS61C : Machine Structures

Lecture Notes on Garbage Collection

HOT-Compilation: Garbage Collection

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

CS 153 Design of Operating Systems

Run-time Environments - 3

Garbage Collection. Vyacheslav Egorov

Cycle Tracing. Presented by: Siddharth Tiwary

Simple Garbage Collection and Fast Allocation Andrew W. Appel

Compiler Construction D7011E

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

Today. Dynamic Memory Allocation: Advanced Concepts. Explicit Free Lists. Keeping Track of Free Blocks. Allocating From Explicit Free Lists

Overview. Introduction to automatic memory management Reference counting BREAK Tracing collectors Automatic memory management in C++

Garbage Collection. CS 351: Systems Programming Michael Saelee

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Internal Fragmentation

JVM Troubleshooting MOOC: Troubleshooting Memory Issues in Java Applications

CSC 1600 Memory Layout for Unix Processes"

Lecture 15 Advanced Garbage Collection

Transcription:

Algorithms for Dynamic Memory Management (236780) Lecture 1 Lecturer: Erez Petrank Class on Tuesdays 10:30-12:30, Taub 9 Reception hours: Tuesdays, 13:30 Office 528, phone 829-4942 Web: http://www.cs.technion.ac.il/~erez/courses/gc!1

Administration Overview on memory management: 3 classic algorithms. Course topics Topics today The Mark & Sweep algorithm!2

Grades Test (closed material). Homework (about 3 (dry) exercises) Type and email to Nachshon Grade: Test - at least 75% Homework at least 10% Participation in class at most 15%.!3

The Book The Garbage Collection Handbook: The Art of Automatic Memory Management by Richard Jones, Anthony Hosking, and Eliot Moss. (Available at the library)!4

The nature of this course This is an applied course. Memory management attracts attention from the programming languages community, the algorithms community, and the systems community. We will tend to focus on the algorithmic aspects, system requirements, and parallelism. We ll mention standard engineering techniques.!5

The course of this course We ll start with simple algorithms. Then, their evolution aiming at typical programs/systems behavior. handling typical platforms Handling platforms: The importance of cache misses Parallel machines Real time requirements Distributed machines One theoretical lower bound The focus: basic ideas and what is in use today.!6

FAQ זה קשה? יש הרבה עבודה? איך הציונים?!7

קורס בחירה משאבים חומר כתוב מלא (ספר) חוברות אין תירגולים, תירגולי חזרה, בחינות פתורות, וידיאו השלמות!8

קורס בחירה הרצאות שקפים תרגילי בית יש!9

עוד אדמיניסטרציה אין חובת נוכחות אבל אין דרך להשלים הרצאות אז אם אי אפשר לבוא להרצאות עדיף לוותר על הקורס יש חובת קדמים אלגוריתמים 1 מערכות הפעלה!10

Memory Management Overview What is it? The classical algorithms!11

Dynamic allocation All programs use local variables --- static allocation (integer i). But sometimes dynamic allocation is required. Usually in more involved programs where length or number of allocated objects depends on the input. Manual dynamic allocation and de-allocation: Ptr = malloc (256 bytes); /* Use ptr */ Free (Ptr);!12

Is manual management good? Practice shows that manual allocation is problematic. 1. Memory leaks. 2. Dangling pointers. In large projects in which objects are shared by various components of the software, it is sometimes difficult to tell when an object is not needed anymore.!13

Solution Automatic memory management: User allocates space for an object. course c = new course(236780) c.class = TAUB 3 Faculty.addCourse(c)!14

Solution Automatic memory management: User allocates space for an object. When system knows the object will not be used anymore, it reclaims its space. Knows? Telling whether an object will be accessed after a given line of code is undecidable. A conservative approximation is used instead.!15

Solution Automatic memory management: User allocates space for an object. When system knows the object will not be used anymore, it reclaims its space. Knows? Reachability: the program does not have a path of pointers from a program variables to the object. (A simple approximation that usually works well.) Other ideas: the compiler can sometimes tell, the user can help with hints, etc.!16

What s good about automatic garbage collection? Software engineering: Relieves users of the book-keeping burden. Stronger reliability, faster debugging. Code understandable and reliable. (Less interaction between modules.) Security (Java): Program never gets a pointer to play with.!17

GC and languages Sometimes it s built in: LISP, Java, C#. The user cannot free an object. Sometimes it s an added feature: C, C++. Most modern languages are supported by garbage collection.!18

Most modern languages rely on GC!19 Source: The Garbage Collection Handbook by Richard Jones, Anthony Hosking, and Eliot Moss.

What s bad about automatic garbage It has a cost: Old Lisp systems 40%. collection? Today s Java program (if the collection is done right ) 5-15%. Considered a major factor determining program efficiency. Techniques have evolved since the 60 s. In this course we investigate the ideas developed from then until now.!20

How have the techniques evolved? Hard to compare collection algorithms. Asymptotic analysis not relevant. Implementation is sometimes more important than the algorithm. But good ideas caught.!21

Note: We discuss memory management in the context of Java. The ideas are useful for other programming languages operating systems memory management disks management, etc.!22

Memory Systems Impact Memory access is the bottleneck (time & energy).!23

The Classical Algorithms!24

Reference counting [Collins 1960] Goal: determine when an object is unreachable from the roots. Roots: local pointers, global pointers, Java Native Interface, etc. Associate a reference count with each object: how many pointers reference this object. When nothing points to an object, it can be deleted. Very simple, used in many systems.!25

Basic Reference Counting Each object has an RC field, new objects get o.rc:=1. When p that points to o 1 p is modified to point to o 2, execute: o 1.RC--, o 2.RC++. o if then o 1.RC==0: 1 o 2 Delete o 1. Decrement o.rc for all children of o 1. Recursively delete objects whose RC is decremented to 0.!26

3 years later [Harold-McBeth 1963] The Reference counting algorithm does not reclaim cycles! But: Normal programs do not use too many cycles. So, other methods are used infrequently to collect the cycles.!27

During the years Many improvements over the basic reference counting method. Cycle collection algorithms. To be discussed in a separate lecture.!28

Mark phase: The Mark-and-Sweep Algorithm [McCarthy 1960] Start from roots and traverse all objects reachable by a path of pointers. Mark all traversed objects. Sweep phase: Go over all objects in the heap. Reclaim objects that are not marked.!29

The Mark-Sweep algorithm Traverse live objects & mark black. White objects can be reclaimed. registers Roots stack Heap!30

Mark-Compact Gradually, the heap gets fragmented. Compaction algorithms compact the heap. We will see several compactors. Issues: Keep order of objects? Use extra space? How many heap passes? Parallelism? The Heap!31

Copying garbage collection Heap partitioned into two. Part 1 takes all allocations. Part 2 is reserved. 1 2 During GC, the collector traces all reachable objects and copies them to the reserved part. After copying, activity goes to part 2. Part 1 is reserved till next collection.!32

Copying garbage collection Part I Part II A B Roots D E C!33

The collection copies Part I Part II A B A C Roots D E C!34

Roots are updated; Part I reclaimed. Part I Part II A C Roots!35

Compaction for free Properties Major disadvantage: half of the heap is not used. Touch only the live objects Good when most objects are dead.!36

A very simplistic comparison Reference Counting Mark & sweep Copying Complexity Pointer updates + dead objects Size of heap (live objects) Live objects Space overhead Count/object + stack for DFS Bit/object + stack for DFS Half heap wasted Compaction Additional work Additional work For free Pause time Mostly short long long More issues Cycle collection!37

Some terms to be remembered Heap, objects Allocate, free (deallocate, delete, reclaim) Reachable, live, dead, unreachable Roots Reference counting, mark and sweep, copying, tracing algorithms Fragmentation!38

Mark & Sweep!39

The basic idea [McCarthy 1960]: 1. Mark all objects reachable from the roots. 2. Scan heap and reclaim unmarked objects.!40 John McCarthy (1927--2011) in PLDI 2002

Triggering New(A)= if no available allocation space mark_sweep() if no available allocation space return ( out-of-memory ) pointer = allocate(a) return (pointer)!41

Basic Algorithm (Cont.) mark_sweep()= for Ptr in Roots mark(ptr) sweep() mark(obj)= if mark_bit(obj) == unmarked mark_bit(obj)=marked for C in Children(Obj) mark(c)!42 Sweep()= p = Heap_bottom while (p < Heap_top) if (mark_bit(p) == unmarked) then free(p) else mark_bit(p) = unmarked; p=p+size(p)

Properties of Mark & Sweep Does not move objects: Conservative collection possible (when pointers cannot be accurately identified). Fragmentation Complexity: Mark phase: live objects (dominant phase) Sweep phase: heap size. Termination: each pointer traversed once. Most popular method today (at a more advanced form).!43

Standard Engineering Techniques Mark: Explicit mark-stack (avoid recursion) Pointer reversal Using bitmaps Sweep: Using bitmaps Lazy sweep!44

Making recursion explicit Problem: if object graph is unsuitable, then recursion becomes too deep. Large space overhead (compiler stack frames) Inefficient execution (function calls) Potential crash (user does not understand why) Solution: use iterative loops and auxiliary data structure instead of functions calls.!45

Modifying mark-roots mark_sweep()= for ptr in Roots mark(ptr) sweep() mark_sweep()= mark_stack = empty for obj referenced by Roots mark_bit (obj) = marked push(obj, mark_stack) Mark_heap() sweep()!46

Modifying heap scan!47 mark_heap()= while mark_stack!= empty obj = pop (mark_stack) for C in children (obj) if mark_bit (C) == unmarked mark_bit (C) = marked push (*C, mark_stack) mark(obj)= if mark_bit(obj) == unmarked mark_bit(obj) = marked for C in Children(Obj) mark(c)

Implementing the stack Standard implementation: linked list of small mark_stacks. Dealing with mark stack overflow: Using cyclic stack (Knuth) or dropping overflowed objects (Bohem et al) After dropping objects, resurrect by searching the heap for mark -> unmarked references.!48

Conclusion (Mark & Sweep) Mark and sweep is a simple method. Advanced versions are common in real systems. Lots of engineering tricks are available. (We ve seen a few.) One of the main obstacles is fragmentation. Our next topic is compaction.!49