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

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

Freeing memory is a pain. Need to decide on a protocol (who frees what?) Pollutes interfaces Errors hard to track down

Freeing memory is a pain. Need to decide on a protocol (who frees what?) Pollutes interfaces Errors hard to track down

Garbage Collection Basics

Programming Languages: Application and Interpretation

Programming Languages: Application and Interpretation

Garbage Collection. Hwansoo Han

Garbage Collection Algorithms. Ganesh Bikshandi

Lecture 13: Garbage Collection

Hard Real-Time Garbage Collection in Java Virtual Machines

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

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

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

Run-Time Environments/Garbage Collection

Garbage Collection (1)

CS61C : Machine Structures

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

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

Heap Compression for Memory-Constrained Java

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

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

CS 241 Honors Memory

Lecture Notes on Garbage Collection

CS61C : Machine Structures

CMSC 330: Organization of Programming Languages

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Lecture Notes on Garbage Collection

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Cycle Tracing. Presented by: Siddharth Tiwary

Dynamic Storage Allocation

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

Run-time Environments -Part 3

Lecture Notes on Advanced Garbage Collection

Automatic Garbage Collection

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

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

Run-time Environments - 3

CMSC 330: Organization of Programming Languages

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

CS61C : Machine Structures

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

CS61C : Machine Structures

CA341 - Comparative Programming Languages

Garbage Collection. Vyacheslav Egorov

CS61C : Machine Structures

Garbage Collection. CS 351: Systems Programming Michael Saelee

HOT-Compilation: Garbage Collection

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

Garbage Collection Techniques

CS61, Fall 2012 Section 2 Notes

Memory Management: The Details

Automatic Memory Management

Shenandoah An ultra-low pause time Garbage Collector for OpenJDK. Christine H. Flood Roman Kennke

G Programming Languages - Fall 2012

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Habanero Extreme Scale Software Research Project

the gamedesigninitiative at cornell university Lecture 9 Memory Management

Memory Allocation III

Memory Allocation III

Garbage Collection. Steven R. Bagley

CS 153 Design of Operating Systems

Introduction to Computer Systems. Exam 2. April 10, Notes and calculators are permitted, but not computers.

Simple Garbage Collection and Fast Allocation Andrew W. Appel

Exploiting the Behavior of Generational Garbage Collector

5. Garbage Collection

Compiler Construction

Programming Language Implementation

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.

Shenandoah: An ultra-low pause time garbage collector for OpenJDK. Christine Flood Principal Software Engineer Red Hat

Lecture 15 Advanced Garbage Collection

Garbage Collection (2) Advanced Operating Systems Lecture 9

Heap Management. Heap Allocation

Name: CIS 341 Final Examination 10 December 2008

LANGUAGE RUNTIME NON-VOLATILE RAM AWARE SWAPPING

Robust Memory Management Schemes

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

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

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Discussion 4 Richard Guo SLAB, Buddy, Garbage Collection, and Intro to MIPS 01/30/09

Compiler Construction

Lecture 15 Garbage Collection

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Dynamic Memory Allocation

Dynamic Memory Allocation: Advanced Concepts

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

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

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Memory Management 3/29/14 21:38

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

Compiler Construction D7011E

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

Memory management. Johan Montelius KTH

Dynamic Memory Allocation: Advanced Concepts

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Dynamic Memory Allocation: Advanced Concepts

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

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

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

Dynamic Memory Management

Transcription:

Garbage Collection Today: various garbage collection strategies; basic ideas: Allocate until we run out of space; then try to free stuff Invariant: only the PL implementation (runtime system) knows about pointers so we can tag everything and find all reachable data (unlike C, C++, asm; like ruby, python, perl, java, racket,... everything else really)

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

Reference Counting Reference counting: a way to know whether a record has other users Attach a count to every record, starting at 0 When installing a pointer to a record increment its count When replacing a pointer to a record, decrement its count When a count reaches 0, decrement counts for other records referenced by the record, then free it 3

Reference Counting Top boxes are the roots Boxes in the blue area are allocated memory 2 4

Reference Counting Adjust counts when a pointer is changed... 0 3 5

Reference Counting... freeing a record if its count goes to 0 2 6

Reference Counting Same if the pointer is a root 0 2 7

Reference Counting Adjust counts after frees, too... 2 0 8

Reference Counting... which can trigger more frees 2 9

Reference Counting And Cycles An assignment can create a cycle... 2 0

Reference Counting And Cycles Adding a reference increments a count 2 2

Reference Counting And Cycles Lower-left records are inaccessible, but not deallocated In general, cycles break reference counting 2 2

Reference counting problems Cycles Maintaining counts wastes time & space Need to use free lists to track available memory (But there are times when this is a good choice) 3

Mark & Sweep Garbage Collection Algorithm Color all records white Color records referenced by roots gray Repeat until there are no gray records: Pick a gray record, r For each white record that r points to, make it gray Color r black Deallocate all white records 4

Mark & Sweep Garbage Collection All records are marked white 5

Mark & Sweep Garbage Collection Mark records referenced by roots as gray 6

Mark & Sweep Garbage Collection Need to pick a gray record Red arrow indicates the chosen record 7

Mark & Sweep Garbage Collection Mark white records referenced by chosen record as gray 8

Mark & Sweep Garbage Collection Mark chosen record black 9

Mark & Sweep Garbage Collection Start again: pick a gray record 20

Mark & Sweep Garbage Collection No referenced records; mark black 2

Mark & Sweep Garbage Collection Start again: pick a gray record 22

Mark & Sweep Garbage Collection Mark white records referenced by chosen record as gray 23

Mark & Sweep Garbage Collection Mark chosen record black 24

Mark & Sweep Garbage Collection Start again: pick a gray record 25

Mark & Sweep Garbage Collection No referenced white records; mark black 26

Mark & Sweep Garbage Collection No more gray records; deallocate white records Cycles do not break garbage collection 27

Mark & Sweep Problems Cost of collection proportional to (entire) heap Bad locality Need to use free lists to track available memory (But there are times when this is a good choice) 28

Two-Space Copying Collectors A two-space copying collector compacts memory as it collects, making allocation easier. Allocator: Partitions memory into to-space and from-space Allocates only in to-space Collector: Starts by swapping to-space and from-space Coloring gray! copy from from-space to to-space Choosing a gray record! walk once though the new to-space, update pointers 29

Two-Space Collection Left = from-space Right = to-space 30

Two-Space Collection Mark gray = copy and leave forward address 3

Two-Space Collection Choose gray by walking through to-space 32

Two-Space Collection Mark referenced as gray 33

Two-Space Collection Mark black = move gray-choosing arrow 34

Two-Space Collection Nothing to color gray; increment the arrow 35

Two-Space Collection Color referenced record gray 36

Two-Space Collection Increment the gray-choosing arrow 37

Two-Space Collection Referenced is already copied, use forwarding address 38

Two-Space Collection Choosing arrow reaches the end of to-space: done 39

Two-Space Collection Right = from-space Left = to-space 40

Two-Space Collection on Vectors Everything is a number: Some numbers are immediate integers Some numbers are pointers An allocated record in memory starts with a tag, followed by a sequence of pointers and immediate integers The tag describes the shape 4

Two-Space Collection on Vectors Use two pointers into the to-space to maintain a queue for a breadth-first traversal Inc the end pointer to add to the queue, increment the front pointer to remove from the queue; when the pointers come together, terminate 42

Two-Space Vector Example 26-byte memory (3 bytes per space), 2 roots Tag : one integer Tag 2: one pointer Tag 3: one integer, then one pointer Root : 7 Root 2: 0 From: 75 2 0 3 2 0 3 2 2 3 4 43

Two-Space Vector Example 26-byte memory (3 bytes per space), 2 roots Tag : one integer Tag 2: one pointer Tag 3: one integer, then one pointer Root : 7 Root 2: 0 From: 75 2 0 3 2 0 3 2 2 3 4 Addr: 00 0 02 03 04 05 06 07 08 09 0 2 44

Two-Space Vector Example 26-byte memory (3 bytes per space), 2 roots Tag : one integer Tag 2: one pointer Tag 3: one integer, then one pointer Root : 7 Root 2: 0 From: 75 2 0 3 2 0 3 2 2 3 4 Addr: 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ 45

Two-Space Vector Example 26-byte memory (3 bytes per space), 2 roots Tag : one integer Tag 2: one pointer Tag 3: one integer, then one pointer Root : 7 Root 2: 0 From: 75 2 0 3 2 0 3 2 2 3 4 Addr: 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ To: 0 0 0 0 0 0 0 0 0 0 0 0 0 Q: 46

Two-Space Vector Example 26-byte memory (3 bytes per space), 2 roots Tag : one integer Tag 2: one pointer Tag 3: one integer, then one pointer Tag 99: forwarding pointer (to to space) Root : 0 Root 2: 0 From: 75 2 0 3 2 0 99 0 2 3 4 Addr: 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ To: 3 2 2 0 0 0 0 0 0 0 0 0 0 Q: ^ ^ 47

Two-Space Vector Example 26-byte memory (3 bytes per space), 2 roots Tag : one integer Tag 2: one pointer Tag 3: one integer, then one pointer Tag 99: forwarding pointer (to to space) Root : 0 Root 2: 3 From: 99 3 2 0 3 2 0 99 0 2 3 4 Addr: 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ To: 3 2 2 75 0 0 0 0 0 0 0 0 Q: ^ ^ 48

Two-Space Vector Example 26-byte memory (3 bytes per space), 2 roots Tag : one integer Tag 2: one pointer Tag 3: one integer, then one pointer Tag 99: forwarding pointer (to to space) Root : 0 Root 2: 3 From: 99 3 99 5 3 2 0 99 0 2 3 4 Addr: 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ To: 3 2 5 75 2 0 0 0 0 0 0 0 Q: ^ ^ 49

Two-Space Vector Example 26-byte memory (3 bytes per space), 2 roots Tag : one integer Tag 2: one pointer Tag 3: one integer, then one pointer Tag 99: forwarding pointer (to to space) Root : 0 Root 2: 3 From: 99 3 99 5 3 2 0 99 0 2 3 4 Addr: 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ To: 3 2 5 75 2 0 0 0 0 0 0 0 Q: ^ ^ 50

Two-Space Vector Example 26-byte memory (3 bytes per space), 2 roots Tag : one integer Tag 2: one pointer Tag 3: one integer, then one pointer Tag 99: forwarding pointer (to to space) Root : 0 Root 2: 3 From: 99 3 99 5 3 2 0 99 0 2 3 4 Addr: 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ To: 3 2 5 75 2 3 0 0 0 0 0 0 Q: ^^ 5

Further reading Uniprocessor Garbage Collection Techniques, by Wilson ftp://ftp.cs.utexas.edu/pub/garbage/gcsurvey.ps 52

Mark and sweep implementation, with linear-time allocator (define (init-allocator) (for ([i (in-range 0 (heap-size))]) (heap-set! i 'free))) (define (gc:flat? loc) (equal? (heap-ref loc) 'flat)) (define (gc:deref loc) (cond [(equal? (heap-ref loc) 'flat) (heap-ref (+ loc ))] [else (error 'gc:deref "non-flat @ ~s" loc)])) 53

Mark and sweep implementation, with linear-time allocator (define (gc:cons? loc) (equal? (heap-ref loc) 'pair)) (define (gc:first pr-ptr) (if (equal? (heap-ref pr-ptr) 'pair) (heap-ref (+ pr-ptr )) (error 'first "non pair @ ~s" pr-ptr))) (define (gc:rest pr-ptr) (if (equal? (heap-ref pr-ptr) 'pair) (heap-ref (+ pr-ptr 2)) (error 'rest "non pair @ ~s" pr-ptr))) 54

Mark and sweep implementation, with linear-time allocator (define (gc:set-first! pr-ptr new) (if (equal? (heap-ref pr-ptr) 'pair) (heap-set! (+ pr-ptr ) new) (error 'set-first! "non pair"))) (define (gc:set-rest! pr-ptr new) (if (equal? (heap-ref pr-ptr) 'pair) (heap-set! (+ pr-ptr 2) new) (error 'set-first! "non pair"))) 55

Mark and sweep implementation, with linear-time allocator (define (gc:alloc-flat fv) (let ([ptr (alloc 2 (if (procedure? fv) (procedure-roots fv) '()) '())]) (heap-set! ptr 'flat) (heap-set! (+ ptr ) fv) ptr)) (define (gc:cons hd tl) (let ([ptr (alloc 3 hd tl)]) (heap-set! ptr 'pair) (heap-set! (+ ptr ) hd) (heap-set! (+ ptr 2) tl) ptr)) 56

Mark and sweep implementation, with linear-time allocator ; a roots is either: ; - root ; - loc ; - (listof roots) ; alloc : number[size] roots roots -> loc (define (alloc n some-roots more-roots) (let ([next (find-free-space 0 n)]) (cond [next next] [else (collect-garbage some-roots more-roots) (let ([next (find-free-space 0 n)]) (unless next (error 'alloc "no space")) next)]))) 57

Mark and sweep implementation, with linear-time allocator ; find-free-space : loc number -> loc or #f (define (find-free-space start size) (cond [(= start (heap-size)) #f] [(n-free-blocks? start size) start] [else (find-free-space (+ start ) size)])) ; n-free-blocks? : loc number -> loc or #f (define (n-free-blocks? start size) (cond [(= size 0) #t] [(= start (heap-size)) #f] [else (and (eq? 'free (heap-ref start)) (n-free-blocks? (+ start ) (- size )))])) 58

Mark and sweep implementation, with linear-time allocator ; collect-garbage : roots roots -> void (define (collect-garbage some-roots more-roots) (mark-white! 0) (traverse/roots (get-root-set)) (traverse/roots some-roots) (traverse/roots more-roots) (free-white! 0)) 59

Mark and sweep implementation, with linear-time allocator ; mark-white! : loc -> void ; marks all records as white, starting with 'i' ; (linear scan of the heap (this linear scan isn't ; really necc but we do it that way for simplicity)) (define (mark-white! i) (when (< i (heap-size)) (case (heap-ref i) [(pair) (heap-set! i 'white-pair) (mark-white! (+ i 3))] [(flat) (heap-set! i 'white-flat) (mark-white! (+ i 2))] [(free) (mark-white! (+ i ))] [else (error 'mark-white! "unknown tag ~s" (heap-ref i))]))) 60

Mark and sweep implementation, with linear-time allocator ; free-white : loc -> void ; frees all white records, starting at 'i' (define (free-white! i) (when (< i (heap-size)) (case (heap-ref i) [(pair) (free-white! (+ i 3))] [(flat) (free-white! (+ i 2))] [(white-pair) (heap-set! i 'free) (heap-set! (+ i ) 'free) (heap-set! (+ i 2) 'free) (free-white! (+ i 3))] [(white-flat) (heap-set! i 'free) (heap-set! (+ i ) 'free) (free-white! (+ i 2))] [(free) (free-white! (+ i ))] [else (error 'free-white! "~s" i)]))) 6

Mark and sweep implementation, with linear-time allocator ; traverse/roots : roots -> void ; traverses the heap, marking ; everything reachable from 'roots' (define (traverse/roots thing) (cond [(list? thing) (for-each traverse/roots thing)] [(root? thing) (traverse/loc (read-root thing))] [(number? thing) (traverse/loc thing)])) 62

Mark and sweep implementation, with linear-time allocator ; traverse/loc : loc -> void ; depth first search for live records (define (traverse/loc loc) (case (heap-ref loc) [(white-pair) (heap-set! loc 'pair) (traverse/loc (heap-ref (+ loc ))) (traverse/loc (heap-ref (+ loc 2)))] [(white-flat) (heap-set! loc 'flat) (let ([val (heap-ref (+ loc ))]) (when (procedure? val) (traverse/roots (procedure-roots val))))] [(pair) (void)] [(flat) (void)] [else (error 'traverse/loc "~s" loc)])) 63