Concepts of programming languages

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
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?
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.
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
CS 536 Spring Automatic Memory Management Lecture 24.
An Efficient Machine-Independent Procedure for Garbage Collection in Various List Structures, Schorr and Waite CACM August 1967, pp Curtis Dunham.
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 355 – Programming Languages
Memory Management Professor Yihjia Tsai Tamkang University.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
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.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
ISBN 0-321— Chapter 6 Data Types. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
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.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
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.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 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.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Reference Counting. Reference Counting vs. Tracing Advantages ✔ Immediate ✔ Object-local ✔ Overhead distributed ✔ Very simple Trivial implementation for.
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.
CSC 533: Programming Languages Spring 2016
Object Lifetime and Pointers
Garbage Collection What is garbage and how can we deal with it?
Data Types In Text: Chapter 6.
CSC 533: Programming Languages Spring 2015
Chapter 6 – Data Types CSCE 343.
Lectures linked lists Chapter 6 of textbook
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Memory Management 6/20/ :27 PM
Dynamic Memory Allocation
Storage Management.
Data Structures Interview / VIVA Questions and Answers
Automatic Memory Management
Concepts of programming languages
Storage.
Simulated Pointers.
Chapter 6 Data Types.
Simulated Pointers.
Strategies for automatic memory management
Chapter 12 Memory Management
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
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
CSC 533: Programming Languages Spring 2019
Reference Counting.
CMPE 152: Compiler Design May 2 Class Meeting
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Concepts of programming languages Chapter 6 Data types Lec. 14 Lecturer: Dr. Emad Nabil

Problems with Pointers 1-Dangling pointers (dangerous) A pointer points to a heap-dynamic variable that has been deallocated 2- Lost heap-dynamic variable memory leakage dangling-pointer Solutions to the Dangling-Pointer Problem Tombstone: extra heap cell that is a pointer to the heap-dynamic variable Locks-and-keys: Pointer values are represented as (key, address) pairs No explicit deallocation of heap-dynamic variables out of the hands of programmers lost heap-dynamic

Heap Management

(eager approach): reclamation is gradual Heap Management A very complex run-time process Single-size cells vs. variable-size cells Two approaches to reclaim garbage reclaim garbage Single-size cells Reference counters (eager approach): reclamation is gradual Mark-sweep (lazy approach) reclamation occurs when the list of available space becomes empty

Reference Counter : Single-size cells Reference counters: maintain a counter in every cell that store the number of pointers currently pointing at the cell. Embedded in the decrement operation for the reference counters, which occurs when a pointer is disconnected from the cell, is a check for a zero value. If the reference counter reaches zero, it means that no program pointers are pointing at the cell, and it has thus become garbage and can be returned to the list of available space. Disadvantages: space required, execution time required. Advantage: efficiency- it is pure incremental, so significant delays in the application execution are avoided

1-Reference Counter : count char * p1 = new char (111); char * p2=p1; 2 111 P1 Dynamic heap variable P2 count 1 222 P1 P1= new char (222); count 1 111 P2 count 1 222 P1 count 1 333 P2= new char (333); P2 count Will be moved to avail_List 0 111

1- Reference Counter : count 2 111 char * p1 = new char (111); P1 char * p2=p1; 2 111 P1 Dynamic heap variable P2 count 0 111 P1 Dynamic heap variable delete p1; P2 Will be moved to avail_List If the reference counter reaches zero, it means that no program pointers are pointing at the cell, and it has thus become garbage and can be returned to the list of available space.

1-Reference Counter // STEP 1 Object a = new Integer (100); Object b = new Integer (99); // Step2 a=b; Possible implementation for reference counter code for a=b; is if (a != b) { if (a != null) if (--a.refCount == 0) heap.release (a); a = b; ++a.refCount; } if (a != b) { if (a != null) --a.refCount; a = b; ++a.refCount; } 

1-Reference Counter – problem public void buidDog() { Dog newDog = new Dog(); Tail newTail = new Tail(); newDog.tail = newTail; newTail.dog = newDog; } Main() { … buidDog(); …. After completing execution of buildDog method the heap will be like this Reference counting does not detect garbage with cyclic references. One of the solution is to use mark and sweep alg.

1-Reference Counter – problem Reference Counting fails to reclaim circular structures Python uses reference counting and offers cycle detection as well.

(eager approach): reclamation is gradual Heap Management reclaim garbage Reference counters (eager approach): reclamation is gradual Mark-sweep (lazy approach) reclamation occurs when the list of available space becomes empty

2-Mark-And-Sweep (Garbage Collection Algorithm) The mark-and-sweep algorithm is called a tracing garbage collector because is traces out the entire collection of objects that are directly or indirectly accessible by the program. The mark-and-sweep algorithm is divided into two phases: Mark phase:  The garbage collector traverses the graph of references from the root nodes and marks each heap object it encounters. Each object has an extra bit: the mark bit – initially the mark bit is 0. It is set to 1 for the reachable objects in the mark phase. Sweep phase: The GC exhaustively scans the heap looking for objects with mark bit 0 – these objects have not been visited in the mark phase – they are garbage. Any such object is added to the free list of objects that can be reallocated. The objects with a mark bit 1 have their mark bit reset to 0.

2-Mark-And-Sweep (Garbage Collection Algorithm)

In java : GC roots are objects that are themselves referenced by the JVM and thus keep every other object from being garbage-collected.

Once all live objects are marked, memory is exhaustively examined to find all of the unmarked (garbage) objects and reclaim their space

2-The Mark-And-Sweep algorithm can be expressed as follows: for each root variable a mark (a); sweep ();  disadvantage: The program must be halted while garbage collection is being performed. void mark (Object a) if (!a.marked) a.marked = true; for each Object b referenced by a mark (b); void sweep () for each Object a in the heap if (a.marked) a.marked = false else heap.release (a); Once all live objects are marked, memory is exhaustively examined to find all of the unmarked (garbage) objects and reclaim their space

Mark-Sweep: Single-size cells The run-time system allocates storage cells as requested and disconnects pointers from cells as necessary; mark-sweep then begins Every heap cell has an extra bit used by collection algorithm All cells initially set to garbage All pointers traced into heap, and reachable cells marked as not garbage All garbage cells returned to list of available cells Disadvantages: in its original form, it was done too infrequently. When done, it caused significant delays in application execution. Modern mark-sweep algorithms avoid this by doing it more often—called incremental mark-sweep Reading slide

Heap Management variable size cells reclaim garbage variable size cells Mark-sweep (lazy approach) reclamation occurs when the list of variable space becomes empty

Variable-Size Cells Required by most programming languages (different data types and structured data types) In case of fixed size cells, we know the size of the garbage, but in case of the variable cell size(ex: char vs double).. How do we know the size of the cell to be reclaimed.. -Solution: each cell have a field to indicate its size. 2. cells with no pointers. How can you follow a chain? Solution: adding internal pointers to every cell. 3. maintaining a list of available space. (how to choose a slot for an object) Solution: sorting the list according to block size, Ascending. Marked (0|1)? Cell size Cell data pointer