Garbage Collection CSCI 2720 Spring 2005. Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
By Jacob SeligmannSteffen Grarup Presented By Leon Gendler Incremental Mature Garbage Collection Using the Train Algorithm.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
CS 536 Spring Automatic Memory Management Lecture 24.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
1 The Compressor: Concurrent, Incremental and Parallel Compaction. Haim Kermany and Erez Petrank Technion – Israel Institute of Technology.
Memory Management Professor Yihjia Tsai Tamkang University.
MOSTLY PARALLEL GARBAGE COLLECTION Authors : Hans J. Boehm Alan J. Demers Scott Shenker XEROX PARC Presented by:REVITAL SHABTAI.
Virtual Memory Primitives for User Programs Andrew W. Appel and Kai Li Presented by: Khanh Nguyen.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Run-Time Storage Organization
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Garbage collection (& Midterm Topics) David Walker COS 320.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Pointers Applications
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
8/14/2015© Hal Perkins & UW CSEW-1 CSE P 501 – Compilers Memory Management & Garbage Collection Hal Perkins Winter 2008.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Memory Allocation CS Introduction to Operating Systems.
Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management.
Chapter 4 Memory Management.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Basic Semantics Associating meaning with language entities.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Memory Management II: Dynamic Storage Allocation Mar 7, 2000 Topics Segregated free lists –Buddy system Garbage collection –Mark and Sweep –Copying –Reference.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Memory Management COSC 513 Presentation Jun Tian 08/17/2000.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
Virtual Memory The memory space of a process is normally divided into blocks that are either pages or segments. Virtual memory management takes.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Memory Management -Memory allocation -Garbage collection.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Memory Management CSCI 2720 Spring What is memory management? “the prudent utilization of this scarce resource (memory), whether by conservation,
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Garbage Collection What is garbage and how can we deal with it?
Names and Attributes Names are a key programming language feature
Memory Allocation The main memory must accommodate both:
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Automatic Memory Management
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Strategies for automatic memory management
Chapter 12 Memory Management
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Run-time environments
CMPE 152: Compiler Design May 2 Class Meeting
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Garbage Collection CSCI 2720 Spring 2005

Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic allocation –Dynamic allocation must be managed 100% by programmer malloc realloc calloc free Lisp –Completely dynamic –Separate programmer from machine

Garbage Collection Sometimes called Automatic Memory Management (OO) Affects design of programs –Tendency to use painless features –Does have cost Part of overall heap management problem Not the only solution Two flavors –Constant sized allocation units –Variable sized allocation units C does not have Garbage Collection!

What is Garbage Collection? Program(mer) requests allocation of memory from heap. If allocation is granted, memory is allocated and address is returned and stored in pointer variable. Contents of pointer variable may be copied so that multiple pointers may exist pointing to same location The allocated area becomes "garbage" if it is no longer being referenced by any pointer. Typically garbage collection occurs when the runtime system no longer has any free memory to allocate

How to Find Garbage Root Set –Set of all pointers that are either global or on activation stack All memory referenced by root set pointers OR by pointers in memory that is referenced by root set pointers Think about a linked list!

Abstract GC Algorithm 1.Stop the machine. 2.Partition the heap into live data and garbage. 3.Mark or rearrange heap so that garbage can be reused. 4.Restart the machine.

When to Garbage Collect? When unable to allocate. When remaining free space is low. Periodically. When user program pauses for terminal or disk I/O. Note: Good news? –Memory is plentiful –Virtual memory makes memory appear larger (cost?) May be worst possible time

How to Decide? Which collector algorithm will be used Whether the application program is interactive How much memory is available on the machine The allocation behavior of the program etc.

Some Typical GC Algorithms Reference Counters Stop and Copy Generational Mark/Sweep

Reference Counters Each allocated block of memory contains a counter. –Each time another pointer starts pointing to the block the counter is incremented –Each time a pointer stops pointing at a block the counter is decremented –If the counter = 0 the block is returned to the free memory list Problems –If the blocks are small the storage taken up by counters becomes significant –Execution time penalty –Circular structures pose difficulties (not insurmountable) Known as the Eager Approach

Stop and Copy Heap is divided into two partitions (to-space and from-space) When GC runs copy all live allocations from the from- space partition to the to-space partition –To-space partition now contains contiguous memory –This will typically run faster on modern hardware (Caches) Swap to-space and from-space (labels) Bad things –Requires twice as much memory –Will repeatedly copy large long-lived things (needlessly)

Generational Overcomes the problem of repeatedly copying large long-lived objects? –Observation: Most allocated data dies young. Idea: Use multiple generation spaces with the to- space of a younger generation equal to the from- space of an older generation. –Collect from from-space 0 to to-space of generation 1. –Collect from generation 1 from-space to generation 2 to- space, etc. Only the oldest generation needs its own to-space. Collect younger generations more frequently than older ones.

Mark & Sweep Language such as Lisp or Scheme based on constant size memory cells: cons cell Cons cell

Internally foobarbaz () fooblarg () barbaz () X Y

But where are free cells?

Free List Free () Allocating a cons cell means getting first cell in free list. Deallocation just reverses the process.

Free List X () Y Free

Clear?

Mark -- Sweep Algorithm Each block must contain bit (mark bit) Initially all blocks are unmarked Starting at each symbol perform a depth-first search marking all blocks reachable (mark means in-use) Sweep through all blocks. –If marked: Unmark –If unmarked: move to free list Note: Algorithm must be only thing running Garbage collection is only done when necessary –i.e. When free list is empty

Mark

Free List X () Y Free Mark

Free List X () Y Free Mark

Free List X () Y Free Mark

Free List X () Y Free Mark

Free List X () Y Free Mark

Free List X () Y Free Mark

Free List X () Y Free Mark

Free List X () Y Free Mark

Free List X () Y Free Mark

Free List X () Y Free Mark

Free List X () Y Free Mark

Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Sweep

Free List X () Y Free Done

Simple? What about variable sized cells?

Variable Sized Cells Have all problems and needs of single-sized cells Have the following additional problems –Sweeping each cell becomes more difficult. Need to have size of each cell at beginning of cell. –Where exactly are the pointers in the cells? One solution is to add system pointers which has extra cost –Free space must be managed as previously discussed

Questions?