Garbage Collection. Steven R. Bagley

Similar documents
Implementation Garbage Collection

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

Lecture Notes on Garbage Collection

CMSC 330: Organization of Programming Languages

CS 241 Honors Memory

CMSC 330: Organization of Programming Languages

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

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

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

Garbage Collection. Hwansoo Han

MEMORY MANAGEMENT HEAP, STACK AND GARBAGE COLLECTION

Lecture 13: Garbage Collection

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

Heap Management. Heap Allocation

Exploiting the Behavior of Generational Garbage Collector

Robust Memory Management Schemes

CS61C : Machine Structures

Garbage Collection (1)

Automatic Memory Management

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

G Programming Languages - Fall 2012

Lecture 15 Garbage Collection

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

Advanced Programming & C++ Language

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

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

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

Run-time Environments -Part 3

Run-Time Environments/Garbage Collection

Programming Language Implementation

Managed runtimes & garbage collection

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

Garbage Collection. Weiyuan Li

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Glacier: A Garbage Collection Simulation System

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

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

Garbage Collection Algorithms. Ganesh Bikshandi

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

Garbage Collection (2) Advanced Operating Systems Lecture 9

CS61C : Machine Structures

CS61C : Machine Structures

Older geometric based addressing is called CHS for cylinder-head-sector. This triple value uniquely identifies every sector.

Distributed Garbage Collection. Distributed Systems Santa Clara University

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

CS61C : Machine Structures

Reference Counting. Steven R. Bagley

Lecture Notes on Garbage Collection

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

Memory Allocation III

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Compiler Construction

Compiler Construction

Automatic Garbage Collection

Dynamic Storage Allocation

Lecture 15 Advanced 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.

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

CS61, Fall 2012 Section 2 Notes

CS61C : Machine Structures

Memory. don t forget to take out the

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

Quantifying the Performance of Garbage Collection vs. Explicit Memory Management

CS61C Midterm Review on C & Memory Management

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

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

The Generational Hypothesis

Garbage Collection. CS 351: Systems Programming Michael Saelee

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

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

Garbage Collection Basics

Structure of Programming Languages Lecture 10

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

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

Java Without the Jitter

Contents. 8-1 Copyright (c) N. Afshartous

Name, Scope, and Binding. Outline [1]

Memory Allocation III

Heapy a memory profiler and debugger for Python

Run-time Environments - 3

Memory Management 3/29/14 21:38

Algorithms for Dynamic Memory Management (236780) Lecture 1

Lecture 7: Binding Time and Storage

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

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

Program Analysis. Uday Khedker. ( uday) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Habanero Extreme Scale Software Research Project

A new Mono GC. Paolo Molaro October 25, 2006

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

Garbage Collection Techniques

Java Performance Tuning

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

Dynamic Memory Management

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

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

Lecture Notes on Advanced Garbage Collection

Dynamic Memory Allocation

CS2210: Compiler Construction. Runtime Environment

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

Configuring the Heap and Garbage Collector for Real- Time Programming.

Transcription:

Garbage Collection Steven R. Bagley

Reference Counting Counts number of pointers to an Object deleted when the count hits zero Eager deleted as soon as it is finished with Problem: Circular references

Reference Counting Saw how we can graft reference counting support onto C++ Programmer must manually retain and release the Can lead to errors Isn t there an automatic method?

Garbage Collection Automatically reclaim memory from garbage (unused s) Several techniques out there Not without its own issues Consider the basic techniques

Reference Counting Common technique ActionScript, Python Language hides the calls to retain/release Still suffers from circular references and thus memory leaks Python tries to find cycles to avoid this

Reference Counting Lightweight Minimal code required to implement Minimal execution time Important advantages in real-time environments

Tracing Collectors Trace-based Garbage Collector Trace over every Look at two types: Mark-sweep collectors Copying collectors

Tracing Collectors Stop-the-world Check to see which s are in use And, by corollary, those which are not Delete the unused ones Restart-the-world

Big Problem How do we know: Which s are in use? (or reachable) How many s there are? In fact, isn t this the problem Garbage Collection is meant to solve Key is in the name Trace-based

In-use s Assume there is a root set of known s Reachable is the root set and any s referenced by the root set And any s referenced by s referenced by the root set Until there are no new s to add to the set of reachable s

root set

root set

root set

root set

root set Objects in bottom-left unreachable from root set

root set Objects in bottom-left unreachable from root set

Root Set All s pointed to by global variables All s pointed to by static class variables All s pointed to by local variables All s pointed to by CPU registers Local variables of this method and all methods up the call stack

All s Can find the s still in use Objects not in use = All minus in use s How do we find all s? Think about how s are created Every created by calling new

Garbage Collector is part of the memory allocator anyway so this is not a problem All s Could store pointers returned from new But there is a simpler way Memory Allocator maintains a list of allocated memory blocks and of free memory Garbage Collector can just use the allocator s list Allocated memory blocks will be the s

Stop The World Important to stop execution of the program If not, program will change state as collector runs Objects may by marked as in-use when they are no longer used

Stop the world Suppose the GC builds the root set Program then moves an pointer from an (a) into a local variable GC traces over reachable s Object (a) never reached and so freed Program accesses (a) and crashes

Mark-Sweep collector Mark-Sweep is a basic form of garbage collector First pass, marks every that is accessible as in-use (by visiting every reachable ) Second pass, visit every allocated and destroy those not marked as in-use

Pass 1 Marking Marks all s that are in-use Already seen how this can be done Start off with s in the root-set and recursively follow pointers to other s Mark as in-use as you visit it

Pass 2 Sweep Stepping through the allocator s list of allocated memory blocks If the is not marked in-use it is destroyed The in-use flag is cleared if it wasn t cleared we d never know if an went out-of-scope

root set Illustrate with laser pointer how s are reached

Mark/Sweep Visits every allocated potentially twice Garbage collection time is proportional to number of s created

Copy Collector Very different approach Splits the memory into two halves or banks All memory allocations occur from one of the banks, the other half is unused

Copy Collector Collector traces from the root set as per usual Rather than marking as in-use, it copies reachable s from one bank of memory to the other Result is that all reachable s are now in the new bank

Copy Collector Collector fixes up pointers to point to new location in memory Allocation is switched to happen from the new bank Next time GC happens it switches back to the previous bank 1st point definitely requires the world stopped or it ll go horrible wrong!

Copy Collector Copy Collector much faster than marksweep Only visits the reachable s Memory allocation becomes trivial Java traditionally uses a Copy Collector Half of memory unused

Conservative GC GC relies on being able to find pointers to s Easy when language designed for GC Structure of (i.e. what is a pointer and what isn t) is known by GC GC can then find pointers in s

Conservative GC Some languages (C++) don t distinguish pointers from other bytes in memory GC then has to be conservative If these bytes could be an pointer, then I ll assume they are Means that s may stay around longer than necessary

Is it an Object Pointer? Get potential pointer from memory Does it point into a block of memory that has been allocated? If so, it is possibly a pointer so mark that block of memory () as in-use If not, not a valid pointer

GC Issues Stop-the-world Program pauses periodically What if something interesting happens during a pause? Modern GC algorithms that allow collection to happen in parallel with program execution

GC Issues Garbage Collection visits every Research has shown that s tend to have short lifetimes Some modern GC algorithms partition the heap into young and old s

GC issues If an exists for a predetermined period, it is moved from the young partition to the old partition Young partition is GC d more regularly than the old partition Less s to visit so quicker GC

Summary Garbage collection Tool for automatically destroying s Two basic types: mark-sweep and Copy collectors Language support GC issues