Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.

Slides:



Advertisements
Similar presentations
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Chapter 6 Data Types
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.
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.
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
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
CS 536 Spring Automatic Memory Management Lecture 24.
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)
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10A - Heap storage Programming Language Design and.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
Run-Time Storage Organization
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
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.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
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.
© 2004, D. J. Foreman 1 Memory Management. © 2004, D. J. Foreman 2 Building a Module -1  Compiler ■ generates references for function addresses may be.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
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.
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.
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.
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Heap storage Dynamic allocation and stacks are generally.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
GARBAGE COLLECTION IN AN UNCOOPERATIVE ENVIRONMENT Hans-Juergen Boehm Computer Science Dept. Rice University, Houston Mark Wieser Xerox Corporation, Palo.
Memory Management -Memory allocation -Garbage collection.
Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
CSC 533: Programming Languages Spring 2016
Storage Allocation Mechanisms
CSC 533: Programming Languages Spring 2015
Storage 18-May-18.
Chapter 6 – Data Types CSCE 343.
Memory Management © 2004, D. J. Foreman.
Storage Management.
Concepts of programming languages
PZ06C - Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3 PZ06C.
Topic 3-b Run-Time Environment
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Heap storage & Garbage collection
List Allocation and Garbage Collection
C H A P T E R F I V E Memory Management.
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Heap storage & Garbage collection
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Automating Memory Management
CSC 533: Programming Languages Spring 2019
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Run-time environments
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001

2 Heap storage Dynamic allocation and stacks are generally incompatible.

3 Stack and heap location Pointer X points to stack storage in procedure Q's activation record that no longer is live (exists) when procedure Q terminates. Such a reference is called a dangling reference. Dynamic storage is usually a separate structure in C, Ada, Pascal...

4 Heap storage problems Major storage for ML, LISP and Prolog is a heap. Get storage - allocate(x) free storage - free(x) Problems: Dangling reference: allocate(x);y = x; free(x);  y still points to allocated Inaccessible storage: allocate(x);  first allocation to x now lost. Memory fragmentation …(next slide)

5 Memory fragmentation Memory fragmentation: allocate(a); allocate(x); allocate(y); free(a); allocate(z); free(y); allocate(b);  No contiguous space for b

6 Garbage collection goal Process to reclaim memory.(Solve Fragmentation problem.) Algorithm: You can do garbage collection if you know where every pointer is in a program. If you move the allocated storage, simply change the pointer to it.  This is true in LISP, ML, Java, Prolog  Not true in C, C++, Pascal, Ada

7 Garbage collection: Mark-sweep algorithm First assume fixed size blocks of size k. (Later blocks of size n*k.)  This is the LISP case. Two simple algorithms follow. (This is only an introduction to this topic. Many other algorithms exist) Algorithm 1: Fixed size blocks; Two pass mark-sweep algorithm: 1. Keep a linked list (free list) of objects available to be allocated. 2. Allocate objects on demand. 3. Ignore free commands. 4. When out of space, perform garbage collection: Pass 1. Mark every object still allocated. Pass 2. Every unmarked objects added to free list of available blocks.

8 Heap storage errors Error conditions: Dangling reference - Cannot occur. Each valid reference will be marked during sweep pass 1. Inaccessible storage - Will be reclaimed during sweep pass 2. Fragmentation - Does not occur; fixed size objects.

9 Garbage collection: reference counts Algorithm 2: 1. Associate a reference counter field, initially 0, with every allocated object. 2. Each time a pointer is set to an object, up the reference counter by Each time a pointer no longer points to an object, decrease the reference counter by If reference counter ever is to 0, then free object. Error conditions: Dangling reference - Cannot occur. Reference count is 0 only when nothing points to it. Fragmentation - Cannot occur. All objects are fixed size. Inaccessible storage - Can still occur, but not easily.

10 Memory compaction: Variable-size elements With variable sized blocks, the previous two algorithms will not work. In the left heap below, a block of size 3 cannot be allocated, even though 7 blocks are free. If the allocated blocks are moved to the start of the heap (compaction) and the free blocks collected, then a block of size up to 7 can be allocated.

11 Compaction algorithm For variable sized blocks, in pass 2 of the mark-sweep algorithm:  Move blocks to top of storage  Need to reset pointers that point to the old storage location to now point to the new storage. How to do this? Use tombstones and two storage areas, an A area and a B area: 1. Fill area A. 2. When A fills, do mark-sweep algorithm. 3. Move all allocated storage to start of B area. Leave marker in A area so other pointers pointing to this object can be modified. 4. After everything moved, area A can discarded. Allocate in B and later compact from B back to A.

12 Compaction algorithm (continued)

13 In place compaction