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.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
(Chapter 5) Deleting Objects
Chapter 6 Data Types
Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.
Garbage Collection CS Introduction to Operating Systems.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection Programming Language Design.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
CSC321: Programming Languages 11-1 Programming Languages Tucker and Noonan Chapter 11: Memory Management 11.1 The Heap 11.2 Implementation of Dynamic Arrays.
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.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Incremental Garbage Collection
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Compilation 2007 Garbage Collection Michael I. Schwartzbach BRICS, University of Aarhus.
Linked lists and memory allocation Prof. Noah Snavely CS1114
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
Memory Management -Memory allocation -Garbage collection.
Introduction to Garbage Collection. Garbage Collection It automatically reclaims memory occupied by objects that are no longer in use It frees the programmer.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
An Efficient, Incremental, Automatic Garbage Collector P. Deutsch and D. Bobrow Ivan JibajaCS 395T.
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.
GARBAGE COLLECTION Student: Jack Chang. Introduction Manual memory management Memory bugs Automatic memory management We know... A program can only use.
CSC 533: Programming Languages Spring 2016
Garbage Collection What is garbage and how can we deal with it?
Core Java Garbage Collection LEVEL – PRACTITIONER.
CSC 533: Programming Languages Spring 2015
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
Storage.
Simulated Pointers.
Simulated Pointers.
Strategies for automatic memory management
Chapter 12 Memory Management
Dynamic Memory.
List Allocation and Garbage Collection
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
CSC 533: Programming Languages Spring 2019
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:

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 to 1. Whenever a new pointer to the heap item is created, increment the counter by 1 Whenever a pointer to the heap item is destroyed, decrement the counter by 1 The heap item is “alive’ so long as the counter is > 0 When the counter decrements to 0, deallocate the heap item

Advantages of Reference Counters It is easy to implement Dangling objects are deallocated the instant they are no longer needed No lengthy periodic algorithm execution Transparent to user

Problems with Reference Counters Space for to store counter variable is wasted for each heap item How do you know how much heap space to deallocate when reclaiming heap space? –Space wasted to store size of heap item. Circular references in heap can cause algorithm to fail

Create Reference to Heap from Stack Heap 1 | | Pointer P Semidynamic Memory Static Memory

Reference Heap 2 from Heap 1 1 | | Heap 1 | | Pointer P Semidynamic Memory Static Memory

Reference Heap 1 from Heap 2 1 | | Heap 2 | | Pointer P Semidynamic Memory Static Memory

Destroy Stack Pointer P 1 | | Heap 1 | | Pointer P Semidynamic Memory Static Memory X

Destroy Stack Pointer P 1 | | Heap 1 | | Semidynamic Memory Static Memory Circular Reference Heap space will never be deallocated

Reference Counts (Example 2) null… Reference Counter (RC) p q free list garbage 0

If p->next = null is Executed 1 12null 000 … Reference Counter (RC) p q free list garbage

Mark and Sweep Mark and Sweep is a garbage collection algorithm Associate a bit called a mark bit with each heap item If mark bit is set to 0, heap item is assumed to be a unreferenced (i.e., a dangling object) If mark bit is set to 1, heap item is assumed to be referenced.

Mark and Sweep Algorithm Mark component of algorithm –Set all heap item mark bits to 0 (unreferenced) –Follow each pointer on the semidynamic stack to heap item they point to and set heap item mark bit to 1 –Recursively follow any pointers in heap items that are set to 1 to other heap items and set their mark bits to 1 –Finish when all possible pointer links originating from semidynamic stack have been exhausted Sweep component of algorithm –Heap items with mark bit set to 0 have no references from the semidynamic stack – they are dangling objects –Go through heap and remove heap items that still have mark bit set to 0

Set All Mark Bits to null… Mark Bit (MB) p q free listnull

Set Accessible Mark Bits to null… Mark Bit (MB) p q free listnull

Sweep (Reclaim Space) null… Mark Bit (MB) p q free list

Advantages of Mark and Sweep All dangling objects will eventually be removed Instead of associating a counter with each item we only need to associate a bit (mark bit) – Therefore, there could be less wasted space (but probably not)

Disadvantages of Mark and Sweep How do you find pointers in heap? They will be at varyingly different locations within the heap item (plain pointer, array of pointers, pointer fields in a structure) How do you find pointers in semidynamic stack? Same problem as in heap How do you know how much heap space to deallocate (same problem as was encountered in reference counters) You invoke algorithm only when you are running out of heap space. As you run out of heap space, performance degrades. Running the mark and sweep algorithm not only degrades performance further, but for quite a long time the CPU is dedicated exclusively to the algorithm. During this period, the screen freezes for an eternity

Stop and Copy (First Half) Use half of heap memory at a time The half in use is in turn divided into two parts –First part contains cells that have been allocated –Second part contains cells available for allocation Memory is allocated sequentially from available cells until it runs out (free space is empty)

Stop and Copy (Second Half) When memory in first half is used up, switch to second half Copy only accessible cells from current half to beginning of second half All activity now switches to second half New memory is allocated starting from first location after the copied space The copying processes touches only accessible heap items

Stop and Copy (First Half) p q null … free list To Space From Space

Stop and Copy (Second Half) p q null … free list To Space From Space …

Other Names Stop and Copy is also referred to as: –Semi-Space Method –Copy Collection

Performance Trade-Offs Assume –R is the number of active heap objects –H is heap size –r is “residency” defined as the ratio of R to the heap size –e is efficiency measured as the amount of memory reclaimed per unit time e is better for Stop and Copy if r is much less than H/2 e is better for Mark and Sweep if r is much greater than H/2

Possible Strategies Dozens of garbage collection algorithms are in use today Most are more complex than the three strategies we have discussed Hybrid Strategies –If R is less than H/2 select Stop and Copy –If R is greater than H/2 select Mark and Sweep

Java Strategies In Sun’s Java system, the garbage collector runs as a low-priority thread The thread executes when processor demand from other threads is low This reduces the number of garbage collection calls during peak demand periods The programmer can explicitly call the garbage collector using the system call System.gc() This invokes garbage collection immediately, regardless of the state of the heap