Implementation Garbage Collection

Similar documents
Garbage Collection. Steven R. Bagley

Programming Language Implementation

Robust Memory Management Schemes

Exploiting the Behavior of Generational Garbage Collector

Garbage Collection. Hwansoo Han

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

Lecture 15 Garbage Collection

Garbage Collection (1)

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

Automatic Memory Management

Run-time Environments -Part 3

Garbage Collection Algorithms. Ganesh Bikshandi

Lecture 13: Garbage Collection

Run-Time Environments/Garbage Collection

Managed runtimes & garbage collection

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

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

Lecture 15 Advanced Garbage Collection

Lecture Notes on Garbage Collection

Myths and Realities: The Performance Impact of Garbage Collection

Automatic Garbage Collection

CMSC 330: Organization of Programming Languages

Name, Scope, and Binding. Outline [1]

Heap Compression for Memory-Constrained Java

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

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

CS 241 Honors Memory

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

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

Garbage Collection Techniques

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

CMSC 330: Organization of Programming Languages

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

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

Cycle Tracing. Presented by: Siddharth Tiwary

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

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

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

A new Mono GC. Paolo Molaro October 25, 2006

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

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

High-Level Language VMs

Compiler Construction

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Compiler Construction

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

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

Heap Management. Heap Allocation

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

G Programming Languages - Fall 2012

Garbage Collection. Weiyuan Li

Hard Real-Time Garbage Collection in Java Virtual Machines

Java Performance Tuning

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

Review. Partitioning: Divide heap, use different strategies per heap Generational GC: Partition by age Most objects die young

Mark-Sweep and Mark-Compact GC

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

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

Run-time Environments - 3

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

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.

Algorithms for Dynamic Memory Management (236780) Lecture 1

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

Garbage Collection (2) Advanced Operating Systems Lecture 9

I J C S I E International Science Press

Lecture Notes on Garbage Collection

Structure of Programming Languages Lecture 10

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

Simple Garbage Collection and Fast Allocation Andrew W. Appel

:.NET Won t Slow You Down

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

CS61C : Machine Structures

Concurrent Garbage Collection

Compiler Construction D7011E

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

The G1 GC in JDK 9. Erik Duveblad Senior Member of Technical Staf Oracle JVM GC Team October, 2017

CPS 506 Comparative Programming Languages. Programming Language

Lecture 13: Complex Types and Garbage Collection

Advances in Programming Languages: Regions

Garbage Collection. Vyacheslav Egorov

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

CA341 - Comparative Programming Languages

CSE 100: C++ TEMPLATES AND ITERATORS

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

Distributed Garbage Collection. Distributed Systems Santa Clara University

Dynamic Storage Allocation

CS Computer Systems. Lecture 8: Free Memory Management

Automatic Memory Management

CS558 Programming Languages

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

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

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

C11: Garbage Collection and Constructors

StackVsHeap SPL/2010 SPL/20

Tick: Concurrent GC in Apache Harmony

06 A Low-Level Virtual Machine

CS558 Programming Languages

The Generational Hypothesis

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

Transcription:

CITS 3242 Programming Paradigms Part IV: Advanced Topics Topic 19: Implementation Garbage Collection Most languages in the functional, logic, and object-oriented paradigms include some form of automatic memory management (including garbage collection). This topic introduces the main concepts involved and the main types of collectors. 1

Garbage Collection Garbage collection is a common term for automatic storage management. Storage management involves allocating memory that a program can use to store data. It also involves reclaiming and reusing this memory when the program no longer needs it technically this part is garbage collection (GC). Traditionally in imperative languages like C and C++, the program must explicitly request memory and later indicate when it is no longer needed. Explicit requests would not fit well with functional/logic languages so these have generally always included automatic management. This allows the programmer to focus on the values involved in their program, without having to worry about low-level details like storage. This also avoids the need to debug a whole class of errors - dangling pointers, overwritten values, access violations. Early GC techniques were inefficient, but recent techniques can match and often beat manual allocation. Mainstream OO languages now also exploit the advantages of GC. Manual allocation is still sometimes necessary for low-level programs. (like building a garbage collector!). 2

Cells and the reachable set Each value is represented in as a block of memory known as a cell A cell contains a tag and one or more fields The tag indicates the type of value (int, array, etc.) and/or the constructor (:: or [] for lists, None, Some, etc.) Each field contains an atomic value (a number) or a pointer (address) The reachable set is all the cells that a program could access in future. This includes the directly accessible roots, which are the CPU registers and the stack of variables for function calls that still need to return. The set also includes every cell pointed to by another one, i.e., all cells that can be reached from the roots by following pointers. During the execution of a program, each cell is in one of three states free, i.e. it has not been allocated food, i.e. it has been allocated and it is holding data that is in use by the program garbage, i.e. it has been allocated, but the data that it holds is no longer in use The free cells are known collectively as the free space of the machine The food cells make up the program graph 3

Storage management tasks The storage manager has two principal tasks The system provides cells to hold the values in new data structures the storage manager must allocate cells from the free-space this process is called storage allocation The system collects garbage cells and adds them to the free-space so that they can be used again the storage manager must find all the garbage cells and arrange them in a data structure this process is called garbage collection Note that the total amount of memory that a program can allocate is unbounded, although the amount that may be food at any given instant is limited by the size of the machine 4

Types of garbage collection algorithms Reference-counting garbage collection The system keeps track of the number of references to each individual cell: when the count for a cell drops to zero, the cell is garbage and is reclaimed garbage cells are reclaimed immediately Tracing garbage collection The system allocates free-space until it is exhausted, then 'saves' ('traces') the food cells and reclaims the rest garbage cells are reclaimed periodically tracing can be achieved in either of two ways: Mark-scan garbage collection The food cells are marked: all unmarked cells are garbage cells and are reclaimed one at a time Copying garbage collection The food cells are moved to a free part of store: the old part of store is now entirely garbage and is reclaimed all at once 5

Comparing garbage collectors Time and space overheads less time spent garbage collecting is better less space for tags, reference counts, etc is better, and generally also avoids the time to update these things. Reference patterns 'tight' reference patterns are better for caching and virtual memory performance many gc's incorporate storage compaction Effect on reaction time the user's program shouldn't be stopped for a long time especially real-time reactive programs An interesting example: reference counting can lead to delays when large data structures become inaccessible. Tracing collectors can generally be done incrementally instead. Scope for parallelism a parallel algorithm should distribute well over several processors 6

Copying garbage collection The store is split into two halves only one half is in use at a given time we call the half in use the current space and the half in reserve the next space the next space is all free note that the next space need not always be in real memory The system allocates cells in the current space until it is full the program is halted and the garbage collector is started (Note: allocation just requires checking and moving a pointer.) The garbage collector copies the program graph (the food cells) from the current space into the next space the current space is now all garbage and can be made free The garbage collector is halted and the program is restarted in the next space 7

Performance of copying garbage collection Time complexity is linear in the number of food cells particularly good when most cells are garbage Space overheads are tolerable need twice as much memory during copying Reference patterns are tight compaction comes for free Allocation is very fast just test and modify the a pointer to the next free word. The effect on reaction time can be severe the program is stopped for the whole garbage collection incremental copying algorithms are available that interleave garbage collection with program execution The scope for parallelism (in incremental algorithms) is good 8

Generational collection Generational collectors use copying collection, but instead of two halves, the store is split into generations. Allocation is performed in the smallest generation, called the nursery. When a generation fills, all live cells in it are copied to the next generation, which is generally somewhat larger. Typical modern generational collectors have 2-3 generations. The oldest (and largest) generation can be collected using any of the gc techniques. Mark and sweep and copying collection are common. Generational collectors quickly filter out long lived data from short-lived garbage, without copying all food cells on every collection. A disadvantage is they require a write-barrier which must record every time an old generation cell is mutated to point to a new generation cell. In practice this cost is usually not a major concern, although sometimes choosing immutable data structures may improve performance. 9

Incremental and parallel collection Garbage collection can lead to pauses while tracing/copying food cells. Many modern collectors use incremental techniques to minimize these, by alternating between running the program and tracing/copying. This idea can be extended to parallel collection, which allows the gc to be running on a separate thread/core/cpu. This reduces pauses, but slightly increases overhead. For servers, often non-incremental collection is preferred. For interactive systems that may regularly be waiting for user input, incremental or parallel collection works particularly well. Current mainstream collectors (CLR, JVM) tend to be generational, with the option of parallel collection for interactive use. 10