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.

Slides:



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

Pointers.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Chapter 6 Data Types
Names and Bindings.
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?
1 Programming Languages Memory Management Chapter 11.
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,
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
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
Ceg860 (Prasad)L9GC1 Memory Management Garbage Collection.
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.
Memory Management Part 2. Data Types Values held in machine locations Integers, reals, characters, Booleans are built into languages as primitive types.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CS 355 – Programming Languages
Memory Management Chapter  Memory management: the process of binding values to (logical) memory locations.  The memory accessible to a program.
1 The Compressor: Concurrent, Incremental and Parallel Compaction. Haim Kermany and Erez Petrank Technion – Israel Institute of Technology.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Run-Time Storage Organization
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
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.
Semantics of Arrays and Pointers By: M. Reza Heydarian Introduction Pointers Arrays Semantics of Arrays Semantics of Pointers.
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
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.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
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.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Memory Management -Memory allocation -Garbage collection.
Spring, 2011 –– Computational Thinking – Dennis Kafura – CS 2984 Data Structures Mapping complex structures to linear memory.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
CSC 533: Programming Languages Spring 2016
Object Lifetime and Pointers
Garbage Collection What is garbage and how can we deal with it?
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Storage Management.
Concepts of programming languages
Simulated Pointers.
Simulated Pointers.
Chapter 12 Memory Management
Dynamic Memory.
Heap storage & Garbage collection
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
Run-time environments
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

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 the first languages to incorporate automatic memory management Key for functional and logic programming languages Many different approaches: Tombstone Locks and Keys Mark-sweep Generational etc.

Heap Run-time stack clarifies our understanding of how memory is organized to implement subprograms Heap helps us understand the run-time behavior of dynamic objects Data Segment static and/or global variables run-time stack (local) space heap (dynamic) space

Static and Run-time Stack Static memory contains values whose: Storage requirements are known before run time Remain constant throughout the life of the program Run-time stack Center of control for dispatching active functions, local variables and parameter-argument linkage

Heap Contains values that are dynamically allocated and structured while the program is running E.g. strings, dynamic arrays, objects, linked-lists By its very nature, becomes fragmented as it is used for dynamic allocation and deallocation of storage blocks of different sizes

Values in the heap For simplicity, assume each memory word in the heap can have one of three states: Unused – not allocated to the program Undef – allocated, but not yet assigned a value Value – e.g. int or float 7undef120 4unused undef0unused h n

Heap management New and delete allow the program to obtain and release a contiguous block of memory words New returns the address of the first word in a contigous block of k-unused works and marks them undef E.g. new(5) h+10 7undef120 4unused undef0 unused h n

Garbage and Dangling Pointers Any block of heap memory that cannot be accessed by the program Easily created: class node { int value; node next; }... node p, q; p = new node(); q = new node(); node p q p q null p q ? delete(p); q=p;

Garbage collection Ideally, we’d never have garbage in the heap However, pinpointing the moment the block is no longer needed by the program can be complex Instead, when the heap becomes full (or some other indicator) we reclaim all blocks that are garbage Three major strategies: Reference counting Mark-sweep Copy collection

Reference Counting Assumes that the initial heap is a continuous chain of nodes called the free list Each node has an additional integer field that contains a count of the number of pointers referencing that node As the program runs, nodes are taken from the free list and connected to each other via pointers “Eager” approach

Reference Counting free_list p q... Reference Count

Algorithm for assignment p = q 1. Reference count for p increased by 1 2. Reference count for q decreased by 1 3. If q’s count is zero, the reference count for each of its descendents is decreased by 1, q’s node returned to free_list a) Repeat for each of q’s descendants 4. Pointer q is assigned the (reference) value of p

Fundamental Flaw? p.next = null; null31 10 p q

Reference Counting Advantage: Occurs dynamically (overhead distributed over the run-time life of the program) Disadvantages: Failure to detect inaccessible circular chains Storage overhead created by appending integer reference counts to every node Performance overhead (each pointer allocation/deallocation)

Mark-Sweep Called into action only when the heap is full Results in two passes through the heap First pass : Mark pass Every heap block that can be reached by following a chain of pointers originating in the run-time stack in marked accessible (mark bit set to 1) Second pass: Sweep pass Returns all unmarked nodes to the free_list Unmarks all nodes that had been marked in mark pass “Lazy” approach

Mark Sweep (initial configuration) 0 null free_list p q... null Mark Bit

Mark Sweep (after first pass) 1 null free_list p q... null

Mark-Sweep (after second pass) 0 null free_list p q...

Mark-Sweep vs. Reference Counting Advantages of mark-sweep Reclaims all garbage in the heap Only invoked when the heap is full Disadvantages: When invoked, takes more time May only result in a small number of cells that can be places on the free_list Variations: Incremental mark-sweep Only do pieces of the heap

Copy Collection A time-space compromise compared to mark-sweep Only called when the heap is full Only makes one pass through the heap Requires much more memory Only half the entire heap space is actively available for allocating new memory blocks

Copy Collection (initial configuration) free p q from_space to_space

Copy Collection (post gc) free p q from_space to_space

Copy Collection vs Mark Sweep Let R : the number of active heap blocks r : the ratio of R to the heap size hs Efficiency : the amount of memory reclaimed per unit time Then: If r is much less than hs/2, copy collection is more efficient As r approaches hs/2, then mark-sweep becomes more efficient

Dangling Pointers Tombstone: extra heap cell that is a pointer to the heap-dynamic variable The actual pointer variable points only at tombstones When heap-dynamic variable de-allocated, tombstone remains but set to nil Costly in time and space Locks-and-keys: Pointer values are represented as (key, address) pairs Heap-dynamic variables are represented as variable plus cell for integer lock value When heap-dynamic variable allocated, lock value is created and placed in lock cell and key cell of pointer

Many different algorithms Hybrid systems that select between mark-sweep and copy collection depending on r Generational Collection: “Recently created regions contain high % of garbage” while older regions do not - Lieberman and Hewitt 85 Like copy-collection, divide heap into multiple regions New objects allocated to the “nursery” Age object “ages” promoted out of the nursery