CPSC 388 – Compiler Design and Construction

Slides:



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

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Chapter 6 Data Types
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free.
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.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
1 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
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.
Memory Management Professor Yihjia Tsai Tamkang University.
CS 61C L07 More Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Memory Management Memory Areas and their use Memory Manager Tasks:
1 Optimizing Malloc and Free Professor Jennifer Rexford
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
CS61C L06 C Memory Management (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS61C Midterm Review on C & Memory Management Fall 2006 Aaron Staley Some material taken from slides by: Michael Le Navtej Sadhal.
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.
CS61C L07 More Memory Management (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Memory Allocation CS Introduction to Operating Systems.
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
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.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
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.
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
Memory Management -Memory allocation -Garbage collection.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
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.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
CSC 533: Programming Languages Spring 2016
Garbage Collection What is garbage and how can we deal with it?
CSC 533: Programming Languages Spring 2015
Storage 18-May-18.
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Storage Management.
Data Structures Interview / VIVA Questions and Answers
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Automatic Memory Management
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Optimizing Malloc and Free
Strategies for automatic memory management
Memory Allocation CS 217.
Tim Ehrlich Growing Arrays in C.
Chapter 12 Memory Management
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
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

CPSC 388 – Compiler Design and Construction Heap Management

Areas of Memory Used by Program Program Code Static Data Heap Stack

Heap Used for dynamically allocated memory Important operations include allocation and de-allocation In C++, Pascal, and Java allocation is done via the “new” operator In C allocation is done via the “malloc” function call De-allocation is done either automatically or programmer must specify when to de-allocate memory: Pascal and C++ – dispose C – free Java – garbage collection

Managing the Heap Available memory is managed using a free list: a list of available “chunks” Each chunk includes: Size of chunk Address of the next item on the free list The chunk itself

Initial Heap Free List Request is made to allocate 20 bytes 0 4 … 103 100 \ … First Free size next Request is made to allocate 20 bytes Uses first portion of first chunk (after size Field) and returns address of 4

Initial Heap Free List Request is made to allocate 10 bytes 0 4 … 23 24 28 … 103 20 \ 76 First Free size size next Request is made to allocate 10 bytes

Initial Heap Free List First chunk is freed 0 4 … 23 24 28 …37 38 42 … 103 20 10 \ 62 First Free size size size next First chunk is freed Adds chunk to front of free list

Initial Heap Free List 0 4 … 23 24 28 …37 38 42 … 103 20 10 \ 62 First 0 4 … 23 24 28 …37 38 42 … 103 20 10 \ 62 First Free size size size next

Operations on Free List Request space Find a satisfactory chunk Free Space Return to Free List Goals for Operations Only fail to satisfy request for n bytes if there are not n bytes available on free list Do both operations quickly

Questions to Consider Given a request for n bytes, which n bytes to return? Given a de-allocation of a chunk, how to coalesce it with neighboring free chunks?

Techniques for Allocation Best Fit: Find the chunk on the freelist with the smallest size greater than or equal to allocation request May require search of entire freelist (SLOW!) Leaves lots of little pieces of free storage on the list

Techniques for Allocation First Fit: Use the first chunk with size greater than or equal to n. Faster than best-fit. Produces little pieces of free storage at the front of the list, which slows later searches

Techniques for Allocation Circular First Fit: Make the freelist circular (i.e. have last item point back to the first item). Satisfy requests using the first chunk with size greater than or equal to n. Change the freelist pointer to point to next chunk after allocated one.

Techniques for de-allocation Use a doubly-linked list Each Chunk has a previous and next pointer One bit of size field reserved to indicated if chunk is “free” or “in-use”. Check free bit of storage after chunk If following chunk is free then coalesce Follow Example on Board

Techniques for De-allocation Can also coalesce with preceding chunk if you keep the size of chunk at beginning and end of chunk Follow example on board Note that NO pointers need to be updated

Automatic or Explicit De-allocation In C++ and C de-allocation must be done explicitly In Java de-allocation is done automatically (by the garbage collector) Making it Automatic reduces burden on the programmer (and eliminates some types of errors)

Errors of Explicit De-allocation Storage Leaks Some storage is never freed even though it is inaccessible Listnode *p = malloc( sizeof(Listnode) ); . . // no copy from p in this code p = ...;

Errors of Explicit De-allocation Dangling pointers A pointer that points to memory that has been freed May read garbage May mess up free list May corrupt other variables

Example Dangling Pointers Listnode *p, *q; p = malloc( sizeof(Listnode) ); q = p; . . // no assignment to q in this code free(p); *q = ...

Detecting Dangling Pointers Add a new field to every allocated chunk (like size field) (lock) Add a new field to every pointer (in addition to storing the address) (key) If lock does not match key then throw an error

Detecting Dangling Pointers Each free chunk’s lock is set to 0 When allocated both lock and key assigned a new value (always increasing) When storage is freed set lock back to zero When pointer is dereferenced, compiler generates code to first match key to lock, otherwise cause error

Automatic De-allocation Determine if a chunk of storage is no longer accessible to the program Make de-allocation efficient, avoid long pauses in program’s execution during de-allocation Two Approaches: Reference Counting Garbage Collection

Reference Counting Include invisible field in every chunk of storage: its reference count field. Value of field is the number of pointers that point to the chunk. Value is initialized to 1 when chunk is allocated and updated: When a pointer is copied, a new reference is created, so the reference count of chunk must be incremented When a non-null pointer’s value is over-written, a reference is removed, so the reference count of the chunk (before the over-write) must be decremented. When a reference count becomes zero, it means nothing points to it so the chunk can be de-allocated and added to free list. If the chunk contains pointers to other chunks, then their reference counts must be decrimented.

Problems with Reference Counting Slows Program Execution Every write into a pointer must test to see if old value is null. Requires updates to reference counts Cyclic Structures cannot be deallocated var p: Nodeptr; /* p is a pointer to a node */ new(p); /* p points to new storage, reference count is 1 */ p^.next = p; /* next field of node points to node, so now reference count is 2 */ p = nil; /* p's value is over-written, so node's reference count decremented(from 2 to 1) In fact, it is inaccessible (it points to itself, no other pointer points to it), but we can't tell that just from the reference count. */

Garbage Collection Wait until no stoarge left then Find all accessible objects Free all other (inaccessible) objects Several Approaches to Garbage Collection Mark and Sweep Stop and Copy

Mark and Sweep Two Phases Mark phase finds and marks all accessible objects Sweep phase sweeps through the heap, collecting all of the garbage and putting back on freelist Another “invisible” value in each chunk called mark bit Initialized to 0 Set to 1 if the chunk is reached during mark phase

Mark Phase Put all “active” pointers on a worklist (“active” means pointer is on stack or static data area) While worklist is not empty do: p=select_pointer(worklist) if p’s object’s mark-bit is zero: change it to one put all pointers in p’s object on worklist

Sweep Phase Looks at every chunk of storage in heap How? If mark-bit for chunk is 0 add to freelist If mark-bit for chunk is 1 change to 0 When adding to freelist coalesce neighbor chunks See example on board

Stop and Copy Garbage Collection Heap is divided into two parts: Old space used for allocation of new chunks New space used for garbage collection First-free pointer points to first free space in old space When allocation request is made for n bytes, if space is available in old space then make allocation, otherwise perform garbage collection

Stop and Copy Garbage Collection Find all accessible objects (following same method as mark and sweep) Copy the object from old space to new space (no mark bit) After making all copies, reverse role of old and new space First-free pointer points to beginning of the “new” old space

Stop and Copy Garbage Collection When chunk is copied from old to new, ALL pointers to chunk must be updated A forwarding pointer is left behind in old space and used to update other pointers to same object Follow example on board

Advantages of Stop and Copy Allocation is Cheaper (no need for searching free list, just advance first-free pointer) No Freelist, just one chunk of free memory, no need to coalesce chunks Cheaper than mark and sweep – no need to scan entire heap Compacting objects means closer together (fewer cache misses, fewer page faults)

Identifying Pointers Automatic deallocation requires the ability to find all pointers on the stack Every word has a one-bit tag (0 for not-pointer, 1 for pointer) Maintain separate bit-map of tags Associate with each variable and each object a type tag.

Summary Two methods of Storage De-allocation Programmer controlled Automatic Programmer controlled errors include: Storage leaks Corrupted memory via dangling pointers Automatic De-allocation Reference counting High space and time overhead Cannot free cyclic structures Cost is distributed over the execution of program Garbage collection Mark and Sweep Stop and Copy