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

Similar documents
Managed runtimes & 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

Garbage Collection. Hwansoo Han

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

CMSC 330: Organization of Programming Languages

Garbage Collection Algorithms. Ganesh Bikshandi

CMSC 330: Organization of Programming Languages

Ulterior Reference Counting: Fast Garbage Collection without a Long Wait

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

Lecture 13: Garbage Collection

Exploiting the Behavior of Generational Garbage Collector

Simple Garbage Collection and Fast Allocation Andrew W. Appel

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

Garbage Collection. Vyacheslav Egorov

A new Mono GC. Paolo Molaro October 25, 2006

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

Garbage Collection Techniques

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

Garbage Collection (1)

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

Programming Language Implementation

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Garbage Collection. Steven R. Bagley

Myths and Realities: The Performance Impact of Garbage Collection

High-Level Language VMs

Garbage Collection. Weiyuan Li

Lecture 15 Garbage Collection

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

Implementation Garbage Collection

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

Write Barrier Removal by Static Analysis

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

Free-Me: A Static Analysis for Automatic Individual Object Reclamation

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

Run-Time Environments/Garbage Collection

JamaicaVM Java for Embedded Realtime Systems

G Programming Languages - Fall 2012

Tick: Concurrent GC in Apache Harmony

Harmony GC Source Code

I J C S I E International Science Press

CS 241 Honors Memory

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

Quantifying the Performance of Garbage Collection vs. Explicit Memory Management

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

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

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

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

Robust Memory Management Schemes

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

Lecture Notes on Garbage Collection

Habanero Extreme Scale Software Research Project

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

Heap Management. Heap Allocation

Java On Steroids: Sun s High-Performance Java Implementation. History

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

A Platform for Research into Object-Level Trace Generation

Azul Systems, Inc.

Heap Compression for Memory-Constrained Java

Dynamic Selection of Application-Specific Garbage Collectors

Field Analysis. Last time Exploit encapsulation to improve memory system performance

Compiler Construction D7011E

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

Hard Real-Time Garbage Collection in Java Virtual Machines

Older-First Garbage Collection in Practice: Evaluation in a Java Virtual Machine

SABLEJIT: A Retargetable Just-In-Time Compiler for a Portable Virtual Machine p. 1

Name, Scope, and Binding. Outline [1]

Software Speculative Multithreading for Java

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

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

Run-time Environments -Part 3

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

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

MEMORY MANAGEMENT HEAP, STACK AND GARBAGE COLLECTION

NG2C: Pretenuring Garbage Collection with Dynamic Generations for HotSpot Big Data Applications

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Mark-Sweep and Mark-Compact GC

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

Running class Timing on Java HotSpot VM, 1

Concurrent Garbage Collection

A Trace-based Java JIT Compiler Retrofitted from a Method-based Compiler

Automatic Garbage Collection

Dynamic Storage Allocation

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

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

Java Performance Tuning

Garbage Collection (2) Advanced Operating Systems Lecture 9

Lecture 13: Complex Types and Garbage Collection

Contents in Detail. Who This Book Is For... xx Using Ruby to Test Itself... xx Which Implementation of Ruby?... xxi Overview...

Lesson 2 Dissecting Memory Problems

CS Computer Systems. Lecture 8: Free Memory Management

Runtime Escape Analysis in a Java Virtual Machine

JVM Troubleshooting MOOC: Troubleshooting Memory Issues in Java Applications

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

the gamedesigninitiative at cornell university Lecture 9 Memory Management

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CS61, Fall 2012 Section 2 Notes

Complex, concurrent software. Precision (no false positives) Find real bugs in real executions

Transcription:

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

Managed runtimes Advantages? Disadvantages? 2

Managed runtimes Advantages? Reliability Security Portability Performance? Disadvantages? Performance? Esp. memory overhead, compilation overhead 3

Portability (& performance) Java source javac Java bytecode?? Native code 4

Portability (& performance) Java source javac Java bytecode Interpreter and/or JVM s JIT compiler(s) Native code 5

Portability (& performance) Java source javac Java bytecode Interpreter and/or JVM s JIT compiler(s) Adaptive optimization Speculative optimizations (e.g., to deal w/class loading) Native code 6

Memory and type safety int[] a = new int[64]; a[82] = ; 7

Memory and type safety int[] a = new int[64]; a[82] = ; C: undefined behavior major source of security exploits (how?) Java: throws ArrayIndexOutOfBoundsException How? Instrumentation added by JIT compiler (or proved unnecessary) 8

Memory and type safety MyType a = new MyType(); *((void*)a + 16) = 42; 9

Memory and type safety MyType a = new MyType(); *((void*)a + 16) = 42; C++: Undefined behavior Java: Pointer arithmetic isn t part of the language 10

Memory and type safety SomeType a = ; b = (IncompatibleType) a; b.f = ; 11

Memory and type safety SomeType a = ; b = (IncompatibleType) a; b.f = ; C: undefined behavior potential security exploit Java: throws ClassCastException How? Instrumentation added by JIT compiler (or proves unnecessary) 12

Memory and type safety MyType a = new MyType(); /* or: malloc(sizeof(mytype)) */ delete a; /* or: free(a) */ a.f = ; 13

Memory and type safety MyType a = new MyType(); delete a; a.f = ; 14

Memory and type safety MyType a = new MyType(); delete a; a.f = ; C++: Undefined behavior Java: Garbage collection no explicit freeing 15

Memory and type safety MyType a = new MyType(); delete a; delete a; 16

Memory and type safety MyType a = new MyType(); delete a; delete a; C++: Undefined behavior Java: Garbage collection no explicit freeing 17

Memory and type safety MyType a = new MyType(); o.f = null; /* Last pointer to a is lost */ 18

Memory and type safety MyType a = new MyType(); o.f = null; /* Last pointer to a is lost */ C++: Memory leak Java: Garbage collection How? Knows all reference types and all roots ; approximates liveness via transitive reachability 19

Why garbage collection? (And how does GC know what s garbage?) Programmers bad at explicit freeing See prior slides for common mistakes Requires global reasoning GC goes hand in hand with memory & type safety GC over approximates liveness via reachability 20

Memory leaks are a real problem Managed languages do not eliminate them

Memory leaks are a real problem Managed languages do not eliminate them Reachable

Memory leaks are a real problem Managed languages do not eliminate them Reachable

Memory leaks are a real problem Managed languages do not eliminate them Dead Reachable Live

Memory leaks are a real problem Managed languages do not eliminate them Dead Reachable Live

Memory leaks are a real problem Managed languages do not eliminate them Reachable Dead Live

Memory leaks are a real problem Managed languages do not eliminate them Dead Live

Memory leaks are a real problem Managed languages do not eliminate them Slow & crash real programs Dead Live

Memory leaks are a real problem Managed languages do not eliminate them Slow & crash real programs Unacceptable for some applications

Memory leaks are a real problem Managed languages do not eliminate them Slow & crash real programs Unacceptable for some applications Fixing leaks is hard Leaks take time to materialize Failure far from cause

GC basics Two types (duals of each other): Reference counting Work proportional to dead objects Memory freed immediately Cycles are problematic Tracing Work proportional to live objects Freeing postponed Can be concurrent 31

How does tracing GC work? Roots Local variables: registers & stack locations Static variables Transitive closure Memory & type safety ensure GC knows the roots and references exactly 32

How does tracing GC work? When does it happen? Stop the world: safe points inserted by VM Concurrent Incremental How many GC threads? Single threaded Parallel 33

Reachability Compiler produces a stack-map at GC safe-points and Type Information Blocks GC safe points: new(), method entry, method exit, & backedges (thread switch points) Stack-map: enumerate global variables, stack variables, live registers -- This code is hard to get right! Why? Type Information Blocks: identify reference fields in objects A B C {... r0 = obj PC -> p.f = obj... globals stack registers heap 34

Reachability Compiler produces a stack-map at GC safe-points and Type Information Blocks Type Information Blocks: identify reference fields in objects for each type i (class) in the program, a map TIB i 0 2 3 A B C {... r0 = obj PC -> p.f = obj... globals stack registers heap 35

Reachability Tracing collector (semispace, marksweep) Marks the objects reachable from the roots live, and then performs a transitive closure over them A B C {... r0 = obj PC -> p.f = obj... mark globals stack registers heap 36

Reachability Tracing collector (semispace, marksweep) Marks the objects reachable from the roots live, and then performs a transitive closure over them A B C {... r0 = obj PC -> p.f = obj... mark globals stack registers heap 37

Reachability Tracing collector (semispace, marksweep) Marks the objects reachable from the roots live, and then performs a transitive closure over them A B C {... r0 = obj PC -> p.f = obj... mark globals stack registers heap 38

Reachability Tracing collector (semispace, marksweep) Marks the objects reachable from the roots live, and then performs a transitive closure over them All unmarked objects are dead, and can be reclaimed A B C {... r0 = obj PC -> p.f = obj... mark globals stack registers heap 39

Reachability Tracing collector (semispace, marksweep) Marks the objects reachable from the roots live, and then performs a transitive closure over them All unmarked objects are dead, and can be reclaimed A B C {... r0 = obj PC -> p.f = obj... sweep globals stack registers heap 40

Example GC algorithms 41

Semispace Fast bump pointer allocation Requires copying collection Cannot incrementally reclaim memory, must free en masse Reserves 1/2 the heap to copy in to, in case all objects are live to space heap from space 42

Semispace Fast bump pointer allocation Requires copying collection Cannot incrementally reclaim memory, must free en masse Reserves 1/2 the heap to copy in to, in case all objects are live to space heap from space 43

Semispace Fast bump pointer allocation Requires copying collection Cannot incrementally reclaim memory, must free en masse Reserves 1/2 the heap to copy in to, in case all objects are live to space heap from space 44

Semispace Fast bump pointer allocation Requires copying collection Cannot incrementally reclaim memory, must free en masse Reserves 1/2 the heap to copy in to, in case all objects are live to space heap from space 45

Semispace Mark phase: copies object when collector first encounters it installs forwarding pointers from space heap to space 46

Semispace Mark phase: copies object when collector first encounters it installs forwarding pointers performs transitive closure, updating pointers as it goes from space heap to space 47

Semispace Mark phase: copies object when collector first encounters it installs forwarding pointers performs transitive closure, updating pointers as it goes from space heap to space 48

Semispace Mark phase: copies object when collector first encounters it installs forwarding pointers performs transitive closure, updating pointers as it goes from space heap to space 49

Semispace Mark phase: copies object when collector first encounters it installs forwarding pointers performs transitive closure, updating pointers as it goes reclaims from space en masse from space heap to space 50

Semispace Mark phase: copies object when collector first encounters it installs forwarding pointers performs transitive closure, updating pointers as it goes reclaims from space en masse start allocating again into to space from space heap to space 51

Semispace Mark phase: copies object when collector first encounters it installs forwarding pointers performs transitive closure, updating pointers as it goes reclaims from space en masse start allocating again into to space from space heap to space 52

Semispace Notice: fast allocation locality of contemporaneously allocated objects locality of objects connected by pointers wasted space from space heap to space 53

Marksweep Free-lists organized by size blocks of same size, or individual objects of same size Most objects are small < 128 bytes 4 8 12 16... 128...... heap free lists 54

Marksweep Allocation Grab a free object off the free list 4 8 12 16... 128...... heap free lists 55

Marksweep Allocation Grab a free object off the free list 4 8 12 16... 128...... heap free lists 56

Marksweep Allocation Grab a free object off the free list 4 8 12 16... 128...... heap free lists 57

Marksweep Allocation Grab a free object off the free list No more memory of the right size triggers a collection Mark phase - find the live objects Sweep phase - put free ones on the free list 4 8 12 16... 128...... heap free lists 58

Marksweep Mark phase Transitive closure marking all the live objects Sweep phase sweep the memory for free objects populating free list 4 8 12 16... 128...... heap free lists 59

Marksweep Mark phase Transitive closure marking all the live objects Sweep phase sweep the memory for free objects populating free list 4 8 12 16... 128...... heap free lists 60

Marksweep Mark phase Transitive closure marking all the live objects Sweep phase sweep the memory for free objects populating free list 4 8 12 16... 128...... heap free lists 61

Marksweep Mark phase Transitive closure marking all the live objects Sweep phase sweep the memory for free objects populating free list can be made incremental by organizing the heap in blocks and sweeping one block at a time on demand 4 8 12 16... 128...... heap free lists 62

Marksweep space efficiency Incremental object reclamation relatively slower allocation time poor locality of contemporaneously allocated objects 4 8 12 16... 128...... heap free lists 63

64

Heap Organization What objects should we put where? Generational hypothesis young objects die more quickly than older ones [Lieberman & Hewitt 83, Ungar 84] most pointers are from younger to older objects [Appel 89, Zorn 90] Organize the heap in to young and old, collect young objects preferentially to space to space from space Young Old 65

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 66

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 67

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 68

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 69

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 70

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 71

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 72

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 73

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 74

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 75

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space to space from space Young Old 76

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces Generalizing to m generations if space n < m fills up, collect n through n-1 to space from space to space Young Old 77

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces - ignore remembered sets Generalizing to m generations if space n < m fills up, collect n through n-1 to space from space to space Young Old 78

Generational Heap Organization Divide the heap in to two spaces: young and old Allocate in to the young space When the young space fills up, collect it, copying into the old space When the old space fills up collect both spaces - ignore remembered sets Generalizing to m generations if space n < m fills up, collect 1 through n-1 to space from space to space Young Old 79

Generational Write Barrier Unidirectional barrier record only older to younger pointers no need to record younger to older pointers, since we never collect the old space independently most pointers are from younger to older objects [Appel 89, Zorn 90] track the barrier between young objects and old spaces address barrier Young Old to space to space from space 80

Generational Write Barrier unidirectional boundary barrier // original program // compiler support for incremental collection p.f = o; if (p > barrier && o < barrier) { remset nursery = remset nursery U &p.f; } p.f = o; Young Old to space to space from space 81

Generational Write Barrier Unidirectional record only older to younger pointers no need to record younger to older pointers, since we never collect the old space independently most pointers are from younger to older objects [Appel 89, Zorn 90] most mutations are to young objects [Stefanovic et al. 99] Young Old to space to space from space 82

How are scalar and array objects represented? TIB GC Locking Hashing field0 field1 field2 objref TIB GC Locking Hashing length element0 element1 objref 83

How does locking work? synchonrized (o) { } Header bits specify: owner thread & depth Biased locking avoids synchronization 84

How does hashing work? o.hashcode(); If calls Object.hashCode() Identity hash code Needs to always return same hash code Dedicated bits Based on address? What if object moves? 85

Other components Classloading Threads and synchronization Exception handling Weak references & finalizers Other questions about how language VMs work? 86