PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 Heap storage Dynamic allocation and stacks are generally.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
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.
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
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.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Management Memory Areas and their use Memory Manager Tasks:
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:
Linked lists and memory allocation Prof. Noah Snavely CS1114
Memory Allocation CS Introduction to Operating Systems.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
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.
Runtime Environments Compiler Construction Chapter 7.
© 2004, D. J. Foreman 1 Memory Management. © 2004, D. J. Foreman 2 Building a Module -1  Compiler ■ generates references for function addresses may be.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
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.
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.
C++ Memory Overview 4 major memory segments Key differences from Java
COMP3190: Principle of Programming Languages
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
Memory Management -Memory allocation -Garbage collection.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
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.
Memory Management.
CSC 533: Programming Languages Spring 2016
Storage Allocation Mechanisms
Memory Management Memory Areas and their use Memory Manager Tasks:
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
Memory Management © 2004, D. J. Foreman.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Memory Management Memory Areas and their use Memory Manager Tasks:
Storage.
CS Introduction to Operating Systems
Memory Allocation CS 217.
PZ09A - Activation records
Topic 3-b Run-Time Environment
Dynamic Memory.
Heap storage & Garbage collection
Name Binding and Object Lifetimes
Memory Management Memory Areas and their use Memory Manager Tasks:
Heap storage & 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
CMPE 152: Compiler Design May 2 Class Meeting
SPL – PS2 C++ Memory Handling.
Presentation transcript:

PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Heap storage Dynamic allocation and stacks are generally incompatible.

PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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...

Looking at allocation in C #define MAXCALL 3 void f(int ncall) { char *memPtr ; // Stack allocation memPtr = (char *)malloc((size_t) 256) ; // Heap allocation printf("\nCall # %d\n", ncall) ; printf("Address of memPtr (on stack): %08x\n", &memPtr) ; printf(" malloc'ed memory (on heap) : %08x\n", memPtr) ; if (ncall <= MAXCALL) f(ncall+1) ; } int main(int argc, char *argv[]) { f(1) ; return 0; }

C Run Time allocation 08049a68 Call # 4 Address of memPtr (on stack): bffffa64 malloc'ed memory (on heap) : 08049a Call # 3 Address of memPtr (on stack): bffffa84 malloc'ed memory (on heap) : Call # 2 Address of memPtr (on stack): bffffaa4 malloc'ed memory (on heap) : Call # 1 Address of memPtr (on stack): bffffac4 malloc'ed memory (on heap) :

Allocation in C++ -- common structure Here’s a class to use in our examples template class Link { public: Elem element; // Value for this node Link *next; // Pointer to next node in list Link(const Elem& elemval, Link* nextval = (Link*)0) { element = elemval; next = nextval; } Link(Link* nextval = (Link*)0) { next = nextval; } }; 150

Allocation in C++ -- static vs. dynamic #define MAXCALL 3 void f(int ncall) { Link staticL(200) ; // Record stored on stack Link *dynamoL ; // Record stored in heap dynamoL = new Link (100) ; printf("\nCall # %d\n", ncall) ; printf("Address of staticL (on stack): %08x\n", &staticL) ; printf("Address of dynamoL (on heap): %08x\n", dynamoL) ; if (ncall <= MAXCALL) f(ncall+1) ; } int main(int argc, char *argv[]) { f(1) ; return 0; }

C++ Run Time Allocation Call # 3 Address of staticL (on stack): bffffa40 Address of dynamoL (on heap): 08049a a50 bfffa a40 bfffa80 Call # 2 Address of staticL (on stack): bffffa80 Address of dynamoL (on heap): 08049a40 Call # 4 Address of staticL (on stack): bffffa00 Address of dynamoL (on heap): 08049a a60 bfffa a30 bfffac0 Call # 1 Address of staticL (on stack): bffffac0 Address of dynamoL (on heap): 08049a30

C++ {de,con}structor Constructor called Implicitly on “declaration” within static scope Explicitly by new Destructor called Implicitly on exit of static scope Explicitly by delete void f(void) { BigTable StaticBT(300) ; BigTable *DynamoBT ; DynamoBT = new BigTable (500) ; delete DynamoBT ; } Classes which allocate dynamic storage need destructors

C++ class with {de,con}structor template class BigTable { private: int tsize; // Size of table Elem *table; // Table pointer public: BigTable(int size = 100) { tsize = size ; table = new Elem[tsize]; } ~BigTable() { delete[] table ; } … other methods }; *table

Garbage pointers in C++ Link *BadStackPointer(void) { Link staticL(200) ; Link *dynamoL ; dynamoL = new Link (100, &staticL) ; return dynamoL ; } DELETED

Garbage pointers in C++ Link *BadHeapPointer(void) { Link *dynamoL1, *dynamoL2 ; dynamoL2 = new Link (200) ; dynamoL1 = new Link (100, dynamoL2) ; delete dynamoL2 ; return dynamoL1 ; } DELETED Link *BadHeapPointer(void) { Link *dynamoL1, *dynamoL2 ; dynamoL2 = new Link (200) ; dynamoL1 = new Link (100, dynamoL2) ; delete dynamoL2 ; return dynamoL1 ; }

Memory leak in C++ Link *LeakMemory(void) { Link *DynamoL1, *DynamoL2, *DynamoL3 ; DynamoL3 = new Link (300) ; DynamoL2 = new Link (200, DynamoL3) ; DynamoL1 = new Link (100, DynamoL2) ; return DynamoL2 ; } LEAKED

Memory overrun in C++ template class BigTable { private: int tsize; // Size of table Elem *table; // Table pointer public: BigTable(int size = 100) { tsize = size ; table = new Elem[tsize]; } ~BigTable() { delete[] table ; } SetLast(Elem& newLast) {table[tsize] = newLast ; } … other methods }; *table

Internal Fragmentation Memory lost inside an allocated block Occurs with fixed-sized allocation is bigger than the data block

External Fragmentation External fragmentation Memory lost outside an allocated block Deleting data may leave a small hole Allocating data within a large hole leaves a small hole

Sequential fit allocation Extra fields track important parameters In allocated blocks Size of allocated block State of preceding block In free blocks Doubly linked list of allocated blocks Size of free block (at end) Used in G++ malloc

Allocation strategies Many slots are big enough, which to use? Best fit Use the smallest Worse fit Use the biggest First fit Use the first For each method, you can find a situation where only that method works

Buddy allocation Memory allocated in blocks of size 2 k Flip the k’th bit to find your buddy 0x08ff8a00 and 0x8ff8b00 are buddies of size 256! When two buddies are freed A larger free block is formed

Page-based bucket allocation Buckets Consist of a set of pages Each pages contains blocks of a fixed size Except some blocks may need several pages Used in operating systems Where page tables may contain bucket identification If you know the address of a block You know its size No wasted space for block size information

Allocation in Compaq tru64 Unix kernel woodfin% vmstat -M bucket# element_size elements_in_use elements_free

PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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

Roots of Garbage Collection Garbage collection begins with a set of roots Pointers stored on the stack In multi-threaded applications roots may be stored on several thread stacks Pointers stored in global data Collection proceeds as a search from the roots Pointers lead to records/structures Records may contain new pointers GC algorithm must be able to recognize pointers In local and global data In records Java Platform Performance: Strategies and Tactics Appendix A

PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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.

PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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.

PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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.

Reference counts in file systems In Unix, files are “addressed” by inode number File reference counts determine if the file has a name When the reference count reaches 0, the file’s data is deleted. A similar facility exists in NTFS 5.0 woodfin% echo "A new file" > Ref1 woodfin% cp Ref1 Copy woodfin% ln Ref1 Ref2 woodfin% ls -li rw-r--r-- 1 brock system 11 Nov 10 16:31 Copy rw-r--r-- 2 brock system 11 Nov 10 16:31 Ref rw-r--r-- 2 brock system 11 Nov 10 16:31 Ref2 woodfin% rm Ref1 woodfin% ls -li rw-r--r-- 1 brock system 11 Nov 10 16:31 Copy rw-r--r-- 1 brock system 11 Nov 10 16:31 Ref2

PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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.

PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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.

PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, LISP storage LISP is first language that makes heavy use of heap storage. Storage reclaimed automatically by LISP environment when out of space. Uses space efficiently. Each LISP item is a fixed size object allocated in the heap. As example shows, although based on a heap, LLISP still needs a stack for execution. Example: Trace execution of: (defun f1(x y z) (cons x (f2 y z ) ) ) (defun f2(v w) (cons v w) )

PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall,