Garbage Collection. Vyacheslav Egorov

Similar documents
Garbage Collection. Hwansoo Han

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

Managed runtimes & garbage collection

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

CMSC 330: Organization of Programming Languages

A new Mono GC. Paolo Molaro October 25, 2006

CMSC 330: Organization of Programming Languages

Garbage Collection Algorithms. Ganesh Bikshandi

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

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

Hard Real-Time Garbage Collection in Java Virtual Machines

Lecture 15 Garbage Collection

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

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Lecture 13: Garbage Collection

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

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

G Programming Languages - Fall 2012

Programming Language Implementation

CS Computer Systems. Lecture 8: Free Memory Management

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

Mark-Sweep and Mark-Compact GC

Concurrent Garbage Collection

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Harmony GC Source Code

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

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

Run-Time Environments/Garbage Collection

Operating System Labs. Yuanbin Wu

Garbage Collection Techniques

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

Lecture Notes on Advanced Garbage Collection

CS 241 Honors Memory

Garbage Collection (2) Advanced Operating Systems Lecture 9

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

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

CS61, Fall 2012 Section 2 Notes

Automatic Memory Management

Lecture 8 Dynamic Memory Allocation

Compiler Construction D7011E

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

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

Hardware-Supported Pointer Detection for common Garbage Collections

Garbage-First Garbage Collection by David Detlefs, Christine Flood, Steve Heller & Tony Printezis. Presented by Edward Raff

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

CS2210: Compiler Construction. Runtime Environment

Implementation Garbage Collection

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

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

Generation scavenging: A non-disruptive high-performance storage reclamation algorithm By David Ungar

Shenandoah: Theory and Practice. Christine Flood Roman Kennke Principal Software Engineers Red Hat

Garbage Collection. CS 351: Systems Programming Michael Saelee

A program execution is memory safe so long as memory access errors never occur:

HOT-Compilation: Garbage Collection

Simple Garbage Collection and Fast Allocation Andrew W. Appel

Lecture 15 Advanced Garbage Collection

Garbage Collection. Steven R. Bagley

Garbage Collection (1)

Garbage Collection. Weiyuan Li

Automatic Garbage Collection

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

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

Exploiting the Behavior of Generational Garbage Collector

CS842: Automatic Memory Management and Garbage Collection. GC basics

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

Dynamic Memory Allocation: Advanced Concepts

G1 Garbage Collector Details and Tuning. Simone Bordet

Ulterior Reference Counting: Fast Garbage Collection without a Long Wait

Tick: Concurrent GC in Apache Harmony

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

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

High Performance Managed Languages. Martin Thompson

MC 2 : High-Performance Garbage Collection for Memory-Constrained Environments

Precise Garbage Collection for C. Jon Rafkind * Adam Wick + John Regehr * Matthew Flatt *

Lecture Conservative Garbage Collection. 3.2 Precise Garbage Collectors. 3.3 Other Garbage Collection Techniques

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Dynamic Memory Allocation

JAVA PERFORMANCE. PR SW2 S18 Dr. Prähofer DI Leopoldseder

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

Dynamic Storage Allocation

Evaluating and improving remembered sets in the HotSpot G1 garbage collector

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

Running class Timing on Java HotSpot VM, 1

Myths and Realities: The Performance Impact of Garbage Collection

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

CS61C : Machine Structures

Robust Memory Management Schemes

CS2210: Compiler Construction. Runtime Environment

IBM Research Report. Efficient Memory Management for Long-Lived Objects

Heap Compression for Memory-Constrained Java

Worksheet #4. Foundations of Programming Languages, WS 2014/15. December 4, 2014

High Performance Managed Languages. Martin Thompson

Understanding Garbage Collection

Parallel Memory Defragmentation on a GPU

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

Compiler Construction

10.1. CS356 Unit 10. Memory Allocation & Heap Management

JVM Memory Model and GC

A Platform for Research into Object-Level Trace Generation

The C4 Collector. Or: the Application memory wall will remain until compaction is solved. Gil Tene Balaji Iyengar Michael Wolf

Transcription:

Garbage Collection Vyacheslav Egorov 28.02.2012

class Heap { public: void* Allocate(size_t sz); };

class Heap { public: void* Allocate(size_t sz); void Deallocate(void* ptr); };

class Heap { public: void* Allocate(size_t sz); void Deallocate(void* ptr); void DeallocateSomething(); };

Something? Some objects that will not be accessed in the future.

Something? Some objects that will not be accessed in the future.

Something? Some objects that will not be accessed in the future.

Something? Some objects that will not be accessed in the future.

Something? Some objects that will not be accessed in the future.

How to allocate?

When to start GC?

How to interact with mutator?

How to interact with runtime system?

How to mark?

How to partition heap?

How to find roots and pointers inside objects?

What to do with live objects?

What to do with dead objects?

Finding pointers Iterating over "thread" roots (local variables) and global roots (global variables, immortal objects) Need to parse the stack if in JIT environment CALLER BP CALLER PC call @func PC determines way to iterate over contents of the stack frame (e.g. bitvector with bit per stack slot) CALLER BP CALLER PC

Finding Pointers Need to know object layout as well (if objects can have mixture of pointers and nonpointers)

In V8 frames{.cc,.h} non-optimized frames contain only tagged pointers optimized code has safepoint information objects.h, object-visiting{.cc,. h} all objects in the heap have a map pointer in their header which points to object describing their layout

In V8 frames{.cc,.h} non-optimized frames contain only tagged pointers optimized code has safepoint information objects.h, object-visiting{.cc,. h} all objects in the heap have a map pointer in their header which points to object describing their layout

In V8: pointer tagging JavaScript is dynamically typed and has primitive numbers. Want to represent (subset) efficiently without boxing every number....00 pointer to 4 bytes aligned object

In V8: pointer tagging JavaScript is dynamically typed and has primitive numbers. Want to represent (subset) efficiently without boxing every number....00 tagged pointer to 4 bytes aligned object enough space to put 1bit tag!

In V8: pointer tagging JavaScript is dynamically typed and has primitive numbers. Want to represent (subset) efficiently without boxing every number....1 tagged pointer to 4 bytes aligned object 31bit int shifted left by 1...0

In V8: pointer tagging JavaScript is dynamically typed and has primitive numbers. Want to represent (subset) efficiently without boxing every number....1 tagged pointer to 4 bytes aligned object intptr_t TagPtr(intptr_t p) { return p + 1; } 31bit int shifted left by 1...0 intptr_t TagInt31(intptr_t i){ return i << 1; }

In V8: pointer tagging can perform arithmetic directly on int31s and check overflow after operations x86 has a powerful addressing mode (ARM not so much) (alternative approach NaN-tagging)

Allocation: toy variant struct GCHeader { /* metainf. */ } void* AllocateObject(size_t sz) { GCHeader* m = (GCHeader*) malloc( sizeof(gcheader) + sz) return (m + 1); } void Reclaim(GCHeader* h) { free(h); }

Allocation: more serious ask raw memory directly from OS mmap(null, N * sysconf(_sc_pagesize), PROT_READ PROT_WRITE, MAP_PRIVATE MAP_ANONYMOUS, -1, 0)

Allocation: more serious bump/free-list allocate inside USED top limit byte* Allocate(size_t sz) { if ((limit - top) < sz) return NULL; byte* old_top = top; top += sz; return old_top; }

Allocation: more serious bump/free-list allocate inside USED FREE USED USED FREE byte* Allocate(size_t sz) { return ListFor(sz)->RemoveFirst(sz); }

In V8 spaces{.cc,.h,-inl.h} MemoryAllocator MemoryChunk, Page, LargePage PagedSpace, FreeList

Stop-the-world MarkSweep Stop mutator Mark all objects from roots Sweep over all objects, freeing dead ones eg put reclaimed space on the free list Start mutator again

Problem: fragmentation Free lists become long and fragmented OS pages become sparsely occupied OS gives us memory with certain granularity

Stop-the-world MarkCompact Stop mutator Mark all object reachable from roots Move all objects "down" can be implemented in different ways, eg three passes Lisp2 Start mutator again.

In V8 mark-compact.{cc,h} MarkCompactCollector

Problem: pauses! Compacting > Marking > Sweeping OK for batch jobs. NOT OK for interactive applications.

Generational Collection NEW SPACE OLD SPACE most objects die young (weak generational hypothesis)

Generational Collection NEW SPACE fast allocation frequent and fast GCs survivors promoted to old space collected without marking through the whole old space

Generational Collection NEW SPACE fast allocation frequent and fast GCs survivors promoted to old space collected without marking through the whole old space

Semispace copying collector TO SPACE FROM SPACE allocation is done in to space when full flip to and from discover&copy life objects from 'from' to 'to' space.

In V8 heap.{cc,h} Heap::Scavenge spaces.{cc,h} NewSpace

Generational Collection NEW SPACE fast allocation frequent and fast GCs survivors promoted to old space collected without marking through the whole old space

Generational Collection pointers to new space can be found efficiently OLD SPACE

Remembered Set Set of slots in the old space that can potentially contain references to young objects.

Remembered Set Set of slots in the old space that can potentially contain references to young objects. // array of slots void** remset[n];

Remembered Set // one bit per word // dirty bit => interesting word bitvector remset; PAGE BITS

Remembered Set // one bit per word // dirty bit => interesting word bitvector remset; BITS PAGE

Card marking // one bit per N words bitvector cards; BITS PAGE

In V8 Prior to version 3.7: card marking spaces.h Page::dirty_regions_ (32 bits per 8kb page) After 3.7: remset, store buffer store-buffer{.cc,.h,-inl.h}

Problem: mutator mutates can create new intergenerational references. how to keep remembered set/cards in sync?

Write Barrier obj.field = value; actually does: *FieldRef(obj, field_idx) = value; RecordWrite( obj, FieldRef(obj, field_idx), value);

Write Barrier void RecordWrite(void* obj, void** slot, void* val) { if (IsNew(val) && IsOld(obj)) { PageOf(obj)->MarkCardFor(slot); } }

Write Barrier void RecordWrite(void* obj, void** slot, void* val) { if (IsNew(val) &&!IsNew(obj)) { PageOf(obj)->MarkCardFor(slot); } }

Write Barrier Page* PageOf(void* p) { return p & ~(kpagesize - 1); } /* no casts for brevity */ trick: pages are kpagesize aligned.

Write Barrier bool IsNew(void* p) { return (p & nmask) == nbase; } trick: new space is 2^N size and 2^N aligned.

Incremental GC Interleave GC steps with mutator activity Tricolor abstraction: objects are white, grey or black white - not seen by GC grey - seen but not scanned black - scaned Write (or read barrier) to maintain invariant RecordWrite(obj, val): if (IsBlack(obj) && IsWhite(val)) BlackToGrey(obj);

Incremental GC Interleaving sweeping is easy. Just don't sweep eagerly.

In V8 incremental-marking{.cc,.h,-inl.h}

Mini Project Ideas Change your Scheme VM to use: Unboxed integers or doubles Generational garbage collector