CS 241 Honors Memory

Similar documents
Garbage Collection. Steven R. Bagley

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

G Programming Languages - Fall 2012

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

Engineering Robust Server Software

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages

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

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

Programming Language Implementation

Run-Time Environments/Garbage Collection

Garbage Collection. Hwansoo Han

CS61C : Machine Structures

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

Heap Management. Heap Allocation

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

RAII and Smart Pointers. Ali Malik

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

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

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

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CA341 - Comparative Programming Languages

CSCI-1200 Data Structures Spring 2018 Lecture 25 Garbage Collection & Smart Pointers

CS61, Fall 2012 Section 2 Notes

Advanced Programming & C++ Language

Name, Scope, and Binding. Outline [1]

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

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

CS61C Midterm Review on C & Memory Management

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Compiler Construction

Exploiting the Behavior of Generational Garbage Collector

Garbage Collection. CS 351: Systems Programming Michael Saelee

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

Implementation Garbage Collection

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

CS558 Programming Languages

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

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

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

Compiler Construction

Lecture 8 Dynamic Memory Allocation

Short Notes of CS201

Structure of Programming Languages Lecture 10

Managed runtimes & garbage collection

CS201 - Introduction to Programming Glossary By

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

Run-time Environments -Part 3

Dynamic Storage Allocation

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

Robust Memory Management Schemes

CS558 Programming Languages

CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

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

Run-time Environments - 3

Object-Oriented Programming for Scientific Computing

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

Habanero Extreme Scale Software Research Project

Dynamic Memory Allocation

THE TROUBLE WITH MEMORY

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CS61C : Machine Structures

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

CS201 Some Important Definitions

CSC 1600 Memory Layout for Unix Processes"

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

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

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Garbage Collection Algorithms. Ganesh Bikshandi

Garbage Collection (1)

Automatic Memory Management

Memory management. Johan Montelius KTH

Lecture 15a Persistent Memory & Shared Pointers

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

Memory Management: The Details

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

Ch. 11: References & the Copy-Constructor. - continued -

Intermediate Programming, Spring 2017*

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

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

Memory Management. Kevin Webb Swarthmore College February 27, 2018

HW 3: Malloc CS 162. Due: Monday, March 28, 2016

CS61C : Machine Structures

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

CS558 Programming Languages

Lecture Notes on Garbage Collection

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

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS61C : Machine Structures

Dynamic Memory Allocation

Exception Safe Coding

CS61C : Machine Structures

CS 241 Honors Concurrent Data Structures

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Transcription:

CS 241 Honors Memory Ben Kurtovic Atul Sandur Bhuvan Venkatesh Brian Zhou Kevin Hong University of Illinois Urbana Champaign February 20, 2018 CS 241 Course Staff (UIUC) Memory February 20, 2018 1 / 35

Overview Memory review Management techniques Heap vs. stack free() alloca() Smart pointers (C++) Garbage collection Reference counting Mark-and-sweep Copying collector Generational In practice Concluding thoughts CS 241 Course Staff (UIUC) Memory February 20, 2018 2 / 35

Types of variables in program memory Static variables Persist throughout the lifetime of the program No runtime cost, but memory cannot be used for anything else CS 241 Course Staff (UIUC) Memory February 20, 2018 3 / 35

Types of variables in program memory Static variables Persist throughout the lifetime of the program No runtime cost, but memory cannot be used for anything else Local variables, function arguments Live for the lifetime of the function call Stored on the stack Allocation and reclaiming of memory is really cheap why? CS 241 Course Staff (UIUC) Memory February 20, 2018 3 / 35

Types of variables in program memory Static variables Persist throughout the lifetime of the program No runtime cost, but memory cannot be used for anything else Local variables, function arguments Live for the lifetime of the function call Stored on the stack Allocation and reclaiming of memory is really cheap why? Heap allocation Slower than stack allocation because of bookkeeping by memory manager This memory is not automatically reclaimed, so will eventually fill up Call free()! But it isn t always obvious when you are done with something CS 241 Course Staff (UIUC) Memory February 20, 2018 3 / 35

malloc() and free() What s the big deal? You all know from using it manual heap memory management is a pain CS 241 Course Staff (UIUC) Memory February 20, 2018 4 / 35

Why is heap management so difficult? Manual bookkeeping (programming error) Dangling pointers, double deletion, memory leaks Don t always know when to clean up memory CS 241 Course Staff (UIUC) Memory February 20, 2018 5 / 35

Why is heap management so difficult? Manual bookkeeping (programming error) Dangling pointers, double deletion, memory leaks Don t always know when to clean up memory Performance tradeoffs with heap memory management Linked list of free blocks, how expensive to get a new block of memory to allocate? Pre-coalesce blocks so allocation can be faster? free becomes expensive CS 241 Course Staff (UIUC) Memory February 20, 2018 5 / 35

Why is heap management so difficult? Manual bookkeeping (programming error) Dangling pointers, double deletion, memory leaks Don t always know when to clean up memory Performance tradeoffs with heap memory management Linked list of free blocks, how expensive to get a new block of memory to allocate? Pre-coalesce blocks so allocation can be faster? free becomes expensive Fragmentation Bad locality of reference Problem in virtual memory systems due to page faults, cache misses CS 241 Course Staff (UIUC) Memory February 20, 2018 5 / 35

free(), continued Keeping mental track of your heap memory gets frustrating in complex programs, especially if you re trying to be careful about error handling a = m a l l o c (... ) ; b = m a l l o c (... ) ; i f ( / do something and i t f a i l s / ) { f r e e ( a ) ; f r e e ( b ) ; return ; } i f ( / do a n o t h e r t h i n g / ) { i f ( / do a t h i r d t h i n g and i t f a i l s / ) { f r e e ( a ) ; f r e e ( b ) ; return ; } /... / } c = m a l l o c (... ) ; i f ( / do a f o u r t h t h i n g and i t f a i l s / ) { f r e e ( c ) ; f r e e ( b ) ; f r e e ( a ) ; return ; } /... / CS 241 Course Staff (UIUC) Memory February 20, 2018 6 / 35

free(), continued (the goto cleanup idiom) a = m a l l o c (... ) ; b = m a l l o c (... ) ; i f ( / do something and i t f a i l s / ) goto cleanup ; i f ( / do a n o t h e r t h i n g / ) { i f ( / do a t h i r d t h i n g and i t f a i l s / ) goto cleanup ; /... / } c = m a l l o c (... ) ; i f ( / do a f o u r t h t h i n g and i t f a i l s / ) goto cleanup ; /... / cleanup : f r e e ( a ) ; f r e e ( b ) ; f r e e ( c ) ; CS 241 Course Staff (UIUC) Memory February 20, 2018 7 / 35

free(), continued (the goto cleanup idiom) a = m a l l o c (... ) ; b = m a l l o c (... ) ; i f ( / do something and i t f a i l s / ) goto cleanup ; i f ( / do a n o t h e r t h i n g / ) { i f ( / do a t h i r d t h i n g and i t f a i l s / ) goto cleanup ; /... / } c = m a l l o c (... ) ; i f ( / do a f o u r t h t h i n g and i t f a i l s / ) goto cleanup ; /... / cleanup : f r e e ( a ) ; f r e e ( b ) ; f r e e ( c ) ; Reduces usage of free() and makes keeping track of variables easier only need to think about it at the end of the function Can be generalized to other resource types (opening/closing files) CS 241 Course Staff (UIUC) Memory February 20, 2018 7 / 35

free(), continued (the goto cleanup idiom) a = m a l l o c (... ) ; b = m a l l o c (... ) ; i f ( / do something and i t f a i l s / ) goto cleanup ; i f ( / do a n o t h e r t h i n g / ) { i f ( / do a t h i r d t h i n g and i t f a i l s / ) goto cleanup ; /... / } c = m a l l o c (... ) ; i f ( / do a f o u r t h t h i n g and i t f a i l s / ) goto cleanup ; /... / cleanup : f r e e ( a ) ; f r e e ( b ) ; f r e e ( c ) ; Reduces usage of free() and makes keeping track of variables easier only need to think about it at the end of the function Can be generalized to other resource types (opening/closing files) Makes things easier, but still have manual heap management CS 241 Course Staff (UIUC) Memory February 20, 2018 7 / 35

So, how can we take advantage of the stack? CS 241 Course Staff (UIUC) Memory February 20, 2018 8 / 35

So, how can we take advantage of the stack? Let s take a look at some of its limitations... CS 241 Course Staff (UIUC) Memory February 20, 2018 8 / 35

alloca() Stack memory is cheap, but often limited by fixed sizes char buffer [512]; puts (" What s your name?"); fgets ( buffer, sizeof ( buffer ), stdin ); CS 241 Course Staff (UIUC) Memory February 20, 2018 9 / 35

alloca() Stack memory is cheap, but often limited by fixed sizes char buffer [512]; puts (" What s your name?"); fgets ( buffer, sizeof ( buffer ), stdin ); What if the person has a really long name? CS 241 Course Staff (UIUC) Memory February 20, 2018 9 / 35

alloca() Stack memory is cheap, but often limited by fixed sizes char buffer [512]; puts (" What s your name?"); fgets ( buffer, sizeof ( buffer ), stdin ); What if the person has a really long name? if (/* entire name wasn t read */) { buffer = realloc ( buffer, 2 * sizeof ( buffer )); fgets (...) ; } CS 241 Course Staff (UIUC) Memory February 20, 2018 9 / 35

alloca() Stack memory is cheap, but often limited by fixed sizes char buffer [512]; puts (" What s your name?"); fgets ( buffer, sizeof ( buffer ), stdin ); What if the person has a really long name? if (/* entire name wasn t read */) { buffer = realloc ( buffer, 2 * sizeof ( buffer )); fgets (...) ; } *** Error in./name : realloc(): invalid pointer: 0x00007fffb3af9250 *** ======= Backtrace: ========= /lib64/libc.so.6(+0x7bc17)[0x7f02fc21dc17] /lib64/libc.so.6(realloc+0x31e)[0x7f02fc2228fe]... Aborted (core dumped) You can t realloc() stack memory CS 241 Course Staff (UIUC) Memory February 20, 2018 9 / 35

alloca() Stack memory is cheap, but often limited by fixed sizes char buffer [512]; puts (" What s your name?"); fgets ( buffer, sizeof ( buffer ), stdin ); What if the person has a really long name? if (/* entire name wasn t read */) { buffer = realloc ( buffer, 2 * sizeof ( buffer )); fgets (...) ; } *** Error in./name : realloc(): invalid pointer: 0x00007fffb3af9250 *** ======= Backtrace: ========= /lib64/libc.so.6(+0x7bc17)[0x7f02fc21dc17] /lib64/libc.so.6(realloc+0x31e)[0x7f02fc2228fe]... Aborted (core dumped) You can t realloc() stack memory... or can you? CS 241 Course Staff (UIUC) Memory February 20, 2018 9 / 35

alloca() Stack memory is cheap, but often limited by fixed sizes char buffer [512]; puts (" What s your name?"); fgets ( buffer, sizeof ( buffer ), stdin ); What if the person has a really long name? if (/* entire name wasn t read */) { buffer = realloc ( buffer, 2 * sizeof ( buffer )); fgets (...) ; } *** Error in./name : realloc(): invalid pointer: 0x00007fffb3af9250 *** ======= Backtrace: ========= /lib64/libc.so.6(+0x7bc17)[0x7f02fc21dc17] /lib64/libc.so.6(realloc+0x31e)[0x7f02fc2228fe]... Aborted (core dumped) You can t realloc() stack memory... or can you? (you can t) CS 241 Course Staff (UIUC) Memory February 20, 2018 9 / 35

alloca() But, there is another way # include < alloca.h> void * alloca ( size_t size ); CS 241 Course Staff (UIUC) Memory February 20, 2018 10 / 35

alloca() But, there is another way # include < alloca.h> void * alloca ( size_t size ); Use it exactly like malloc(), but memory is allocated on the stack rather than heap No free() necessary! (It would crash, like before) Thus, memory leaks are impossible Cheap and fast (no heap memory management) CS 241 Course Staff (UIUC) Memory February 20, 2018 10 / 35

alloca() But, there is another way # include < alloca.h> void * alloca ( size_t size ); Use it exactly like malloc(), but memory is allocated on the stack rather than heap No free() necessary! (It would crash, like before) Thus, memory leaks are impossible Cheap and fast (no heap memory management) What s wrong with this? CS 241 Course Staff (UIUC) Memory February 20, 2018 10 / 35

alloca() But, there is another way # include < alloca.h> void * alloca ( size_t size ); Use it exactly like malloc(), but memory is allocated on the stack rather than heap No free() necessary! (It would crash, like before) Thus, memory leaks are impossible Cheap and fast (no heap memory management) What s wrong with this? Stack scoping rules: can t return a pointer to stack space! Overflow: stack space is limited; can t allocate too much memory CS 241 Course Staff (UIUC) Memory February 20, 2018 10 / 35

alloca() But, there is another way # include < alloca.h> void * alloca ( size_t size ); Use it exactly like malloc(), but memory is allocated on the stack rather than heap No free() necessary! (It would crash, like before) Thus, memory leaks are impossible Cheap and fast (no heap memory management) What s wrong with this? Stack scoping rules: can t return a pointer to stack space! Overflow: stack space is limited; can t allocate too much memory Non-standard! (Not part of ANSI C, POSIX, etc...) CS 241 Course Staff (UIUC) Memory February 20, 2018 10 / 35

alloca() But, there is another way # include < alloca.h> void * alloca ( size_t size ); Use it exactly like malloc(), but memory is allocated on the stack rather than heap No free() necessary! (It would crash, like before) Thus, memory leaks are impossible Cheap and fast (no heap memory management) What s wrong with this? Stack scoping rules: can t return a pointer to stack space! Overflow: stack space is limited; can t allocate too much memory Non-standard! (Not part of ANSI C, POSIX, etc...) So, not a good idea CS 241 Course Staff (UIUC) Memory February 20, 2018 10 / 35

C++ smart pointers New feature in C++11 standard library (although concept existed before) CS 241 Course Staff (UIUC) Memory February 20, 2018 11 / 35

C++ smart pointers New feature in C++11 standard library (although concept existed before) Concept of ownership: CS 241 Course Staff (UIUC) Memory February 20, 2018 11 / 35

C++ smart pointers New feature in C++11 standard library (although concept existed before) Concept of ownership: Instead of passing pointers around like mad, let s set some rules CS 241 Course Staff (UIUC) Memory February 20, 2018 11 / 35

C++ smart pointers New feature in C++11 standard library (although concept existed before) Concept of ownership: Instead of passing pointers around like mad, let s set some rules RAII (Resource Acquisition Is Initialization) remember from CS 225, maybe? Only allocate memory in your constructor and free it in your destructor Put things on the stack as much as possible CS 241 Course Staff (UIUC) Memory February 20, 2018 11 / 35

C++ smart pointers New feature in C++11 standard library (although concept existed before) Concept of ownership: Instead of passing pointers around like mad, let s set some rules RAII (Resource Acquisition Is Initialization) remember from CS 225, maybe? Only allocate memory in your constructor and free it in your destructor Put things on the stack as much as possible Smart pointer wraps an ordinary (raw) pointer and defines the semantics for how it is managed and who s managing it Using these rules, you should (almost) never need to manually new/delete memory CS 241 Course Staff (UIUC) Memory February 20, 2018 11 / 35

C++ smart pointers Types of smart pointers std::unique_ptr: Exactly one owner (compile-time error to try to copy it) When that owner lets the pointer go out of scope, the memory is automatically released CS 241 Course Staff (UIUC) Memory February 20, 2018 12 / 35

C++ smart pointers Types of smart pointers std::unique_ptr: Exactly one owner (compile-time error to try to copy it) When that owner lets the pointer go out of scope, the memory is automatically released std::shared_ptr: Reference-counted version (we ll discuss what this means later) In other words, it can have multiple owners, and is released when the last one lets go CS 241 Course Staff (UIUC) Memory February 20, 2018 12 / 35

C++ smart pointers Types of smart pointers std::unique_ptr: Exactly one owner (compile-time error to try to copy it) When that owner lets the pointer go out of scope, the memory is automatically released std::shared_ptr: Reference-counted version (we ll discuss what this means later) In other words, it can have multiple owners, and is released when the last one lets go std::weak_ptr: Stores a temporary reference without owning it Can be used to break cycles if two objects refer to each other (e.g., doubly linked lists) Useful for caches, which store references to objects that should be deleted if the cache is the only thing still refering to it CS 241 Course Staff (UIUC) Memory February 20, 2018 12 / 35

C++ smart pointers Old way: b o o l f u n c ( ) { s i z e t b u f s i z e = 1024 1024 1 2 8 ; i n t data = new i n t [ b u f s i z e ] ; } f o r ( i n t i = 0 ; i < b u f s i z e ; i ++) { data [ i ] = g e t d a t a ( ) ; i f (! data [ i ] ) { d e l e t e [ ] data ; r e t u r n f a l s e ; } } // do something f a n c y w i t h data d e l e t e [ ] data ; r e t u r n true ; CS 241 Course Staff (UIUC) Memory February 20, 2018 13 / 35

C++ smart pointers Old way: b o o l f u n c ( ) { s i z e t b u f s i z e = 1024 1024 1 2 8 ; i n t data = new i n t [ b u f s i z e ] ; } f o r ( i n t i = 0 ; i < b u f s i z e ; i ++) { data [ i ] = g e t d a t a ( ) ; i f (! data [ i ] ) { d e l e t e [ ] data ; r e t u r n f a l s e ; } } // do something f a n c y w i t h data d e l e t e [ ] data ; r e t u r n true ; Using std::unique_ptr: b o o l f u n c ( ) { s i z e t b u f s i z e = 1024 1024 1 2 8 ; auto data = s t d : : make unique<i n t [] >( b u f s i z e ) ; } f o r ( i n t i = 0 ; i < b u f s i z e ; i ++) { data [ i ] = g e t d a t a ( ) ; i f (! data [ i ] ) r e t u r n f a l s e ; } // do something f a n c y w i t h data r e t u r n true ; CS 241 Course Staff (UIUC) Memory February 20, 2018 13 / 35

C++ smart pointers Old way: b o o l f u n c ( ) { s i z e t b u f s i z e = 1024 1024 1 2 8 ; i n t data = new i n t [ b u f s i z e ] ; } f o r ( i n t i = 0 ; i < b u f s i z e ; i ++) { data [ i ] = g e t d a t a ( ) ; i f (! data [ i ] ) { d e l e t e [ ] data ; r e t u r n f a l s e ; } } // do something f a n c y w i t h data d e l e t e [ ] data ; r e t u r n true ; Using std::unique_ptr: b o o l f u n c ( ) { s i z e t b u f s i z e = 1024 1024 1 2 8 ; auto data = s t d : : make unique<i n t [] >( b u f s i z e ) ; } f o r ( i n t i = 0 ; i < b u f s i z e ; i ++) { data [ i ] = g e t d a t a ( ) ; i f (! data [ i ] ) r e t u r n f a l s e ; } // do something f a n c y w i t h data r e t u r n true ; No calls to new[] or delete[] necessary C++ will automatically allocate the memory we want when we call std::make_unique, and release it when the unique pointer goes out of scope when the function exits CS 241 Course Staff (UIUC) Memory February 20, 2018 13 / 35

C++ smart pointers...but not everyone uses C++ CS 241 Course Staff (UIUC) Memory February 20, 2018 14 / 35

What about when you need the heap? You can t always rely on the stack because it can overflow quickly How do you easily manage large objects? CS 241 Course Staff (UIUC) Memory February 20, 2018 15 / 35

What about when you need the heap? You can t always rely on the stack because it can overflow quickly How do you easily manage large objects? Garbage collection to the rescue! - Automagic Memory Management CS 241 Course Staff (UIUC) Memory February 20, 2018 15 / 35

GC: Reference counting Each object has count of how many objects point to it Need to track count whenever object pointers are set or removed/freed CS 241 Course Staff (UIUC) Memory February 20, 2018 16 / 35

Demo Python! CS 241 Course Staff (UIUC) Memory February 20, 2018 17 / 35

GC: Reference Counting Problems Overhead of maintaining count CS 241 Course Staff (UIUC) Memory February 20, 2018 18 / 35

GC: Reference Counting Problems Overhead of maintaining count Lumpy deletions: tend to have clusters of related objects. CS 241 Course Staff (UIUC) Memory February 20, 2018 18 / 35

GC: Reference Counting Problems Overhead of maintaining count Lumpy deletions: tend to have clusters of related objects. Cycles CS 241 Course Staff (UIUC) Memory February 20, 2018 18 / 35

GC: Tracing Most common form of garbage collection CS 241 Course Staff (UIUC) Memory February 20, 2018 19 / 35

GC: Tracing Most common form of garbage collection Trace connections between references and allocations CS 241 Course Staff (UIUC) Memory February 20, 2018 19 / 35

GC: Tracing Most common form of garbage collection Trace connections between references and allocations CS 241 Course Staff (UIUC) Memory February 20, 2018 19 / 35

GC: Mark-and-Sweep Two Step Process CS 241 Course Staff (UIUC) Memory February 20, 2018 20 / 35

GC: Mark-and-Sweep Two Step Process Mark - Find and label all accessible objects CS 241 Course Staff (UIUC) Memory February 20, 2018 20 / 35

GC: Mark-and-Sweep Two Step Process Mark - Find and label all accessible objects Follow all pointers from the root set. Perform DFS on pointers Flag field set to marked CS 241 Course Staff (UIUC) Memory February 20, 2018 20 / 35

GC: Mark-and-Sweep Sweep - Remove all inaccessible objects CS 241 Course Staff (UIUC) Memory February 20, 2018 21 / 35

GC: Mark-and-Sweep Sweep - Remove all inaccessible objects Iterate through memory If marked, unmark Else, free CS 241 Course Staff (UIUC) Memory February 20, 2018 21 / 35

GC: Mark-and-Sweep CS 241 Course Staff (UIUC) Memory February 20, 2018 22 / 35

GC: Mark-and-Sweep Pros CS 241 Course Staff (UIUC) Memory February 20, 2018 23 / 35

GC: Mark-and-Sweep Pros Works with cyclic data structures Effective and easy to implement CS 241 Course Staff (UIUC) Memory February 20, 2018 23 / 35

GC: Mark-and-Sweep Pros Works with cyclic data structures Effective and easy to implement Cons CS 241 Course Staff (UIUC) Memory February 20, 2018 23 / 35

GC: Mark-and-Sweep Pros Works with cyclic data structures Effective and easy to implement Cons Program must halt Fragmentation CS 241 Course Staff (UIUC) Memory February 20, 2018 23 / 35

GC: Mark-and-Sweep with Compaction Alleviates fragmentation Sweep moves memory to the left CS 241 Course Staff (UIUC) Memory February 20, 2018 24 / 35

GC: Mark-and-Sweep with Compaction Alleviates fragmentation Sweep moves memory to the left CS 241 Course Staff (UIUC) Memory February 20, 2018 24 / 35

GC: Tri-Color Marking Incremental garbage collector CS 241 Course Staff (UIUC) Memory February 20, 2018 25 / 35

GC: Tri-Color Marking Incremental garbage collector Gray Set - Need to be scanned blocks Black Set - Accessible blocks White Set - Inaccessible blocks CS 241 Course Staff (UIUC) Memory February 20, 2018 25 / 35

GC: Tri-Color Marking Incremental garbage collector Gray Set - Need to be scanned blocks Black Set - Accessible blocks White Set - Inaccessible blocks All blocks accessible from the root start in the gray set. All others start in the white set. Gray moves to black, all white set objects referenced moved to gray. CS 241 Course Staff (UIUC) Memory February 20, 2018 25 / 35

Generational GC Do we really need to scan everything all the time? Split the objects up into generations Scan each generation with a different frequency CS 241 Course Staff (UIUC) Memory February 20, 2018 26 / 35

Generational GC - Eden Compaction not important, needs fast GC When full, run mark phase and move objects up CS 241 Course Staff (UIUC) Memory February 20, 2018 27 / 35

Generational GC - Survivor Space 1 Count for objects being copied back and forth When full, run mark phase and move objects up CS 241 Course Staff (UIUC) Memory February 20, 2018 28 / 35

Generational GC - Survivor Space 2 Compaction not important Less frequent GC CS 241 Course Staff (UIUC) Memory February 20, 2018 29 / 35

Generational GC - Old Generation Mark/sweep with compaction since objects live longer Run less frequently, since slow process CS 241 Course Staff (UIUC) Memory February 20, 2018 30 / 35

Generational GC - Problems Objects in old generation could be pointing to objects in survivor space Mark phase in survivor space does not go through objects in old generation Maintain separate table for maintaining such pointers across generations Turns out such occurrences are less frequent in code bases Objects in old generation don t generally change much, so may not be too expensive in practice Add references from this table to root set of copying GC running in survivor space What about younger older generation references? CS 241 Course Staff (UIUC) Memory February 20, 2018 31 / 35

Garbage collection in C/C++? Doesn t come with one for performance reasons! Usually a bolted-on third party library Link the library and allow it to reclaim unused memory automatically malloc can be replaced with garbage collector s allocator, and free is a no-op CS 241 Course Staff (UIUC) Memory February 20, 2018 32 / 35

Garbage collection in C/C++? CS 241 Course Staff (UIUC) Memory February 20, 2018 33 / 35

GC doesn t mean you forget about memory Explicitly let go of object by setting the reference to NULL. This is useful with global variables. Otherwise you hang onto system resources longer. Need to manage other system resources (like files); they are closed before GC by programmer CS 241 Course Staff (UIUC) Memory February 20, 2018 34 / 35

Conclusion Thank you! Questions? CS 241 Course Staff (UIUC) Memory February 20, 2018 35 / 35