Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
(Chapter 5) Deleting Objects
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
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.
Memory Management Tom Roeder CS fa. Motivation Recall unmanaged code eg C: { double* A = malloc(sizeof(double)*M*N); for(int i = 0; i < M*N; i++)
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.
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.
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)
Memory Management Professor Yihjia Tsai Tamkang University.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Age-Oriented Concurrent Garbage Collection Harel Paz, Erez Petrank – Technion, Israel Steve Blackburn – ANU, Australia April 05 Compiler Construction Scotland.
Garbage collection (& Midterm Topics) David Walker COS 320.
Linked lists and memory allocation Prof. Noah Snavely CS1114
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.
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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Memory Management II: Dynamic Storage Allocation Mar 7, 2000 Topics Segregated free lists –Buddy system Garbage collection –Mark and Sweep –Copying –Reference.
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.
Memory Management. Memory  Commemoration or Remembrance.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
Copyright © Genetic Computer School 2008 Computer Systems Architecture SA 7- 0 Lesson 7 Memory Management.
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.
More Distributed Garbage Collection DC4 Reference Listing Distributed Mark and Sweep Tracing in Groups.
GARBAGE COLLECTION IN AN UNCOOPERATIVE ENVIRONMENT Hans-Juergen Boehm Computer Science Dept. Rice University, Houston Mark Wieser Xerox Corporation, Palo.
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)
CSC 213 – Large Scale Programming. Explicit Memory Management  Traditional form of memory management  Used a lot, but fallen out of favor  malloc /
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Introduction to Garbage Collection. Garbage Collection It automatically reclaims memory occupied by objects that are no longer in use It frees the programmer.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
GARBAGE COLLECTION Student: Jack Chang. Introduction Manual memory management Memory bugs Automatic memory management We know... A program can only use.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Object Lifetime and Pointers
Garbage Collection What is garbage and how can we deal with it?
Dynamic Memory Allocation
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Automatic Memory Management
Storage.
Simulated Pointers.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Simulated Pointers.
Memory Management Kathryn McKinley.
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
CS703 - Advanced Operating Systems
CMPE 152: Compiler Design May 2 Class Meeting
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Memory Allocation and Garbage Collection

Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know memory requirements in advance when the program is written. In early Fortran all memory had to be allocated in advance. There was no dynamic memory In early Fortran all memory had to be allocated in advance. There was no dynamic memory You can request memory dynamically using malloc/new. You can request memory dynamically using malloc/new.

Types of Memory Management Dynamic memory needs to be recycled after it is no longer in use or the system may run out of memory. Dynamic memory needs to be recycled after it is no longer in use or the system may run out of memory. There are two ways memory can be recycled: There are two ways memory can be recycled: –Explicit Memory Management: »The program calls free/delete explicitly –Automatic Memory Management »The system determines what can be recycled using Automatic Garbage Collection

Explicit Memory Management The program calls free/delete when object is no longer in use. The program calls free/delete when object is no longer in use. Advantages: Advantages: –It uses less memory. The program do not need to wait to recycle memory. –Faster. No GC overhead and the memory allocated is likely to be in cache.

Explicit Memory Management Disadvantages: Disadvantages: –Error prone –Memory leaks - Memory is never freed. It causes system slow down or running out of memory swap space. Bad 24/7 apps. –Premature Frees – Memory is freed while still in use. It causes the program to crash. –Double frees – Free an object that is already freed. –Free of Non-Heap objects – Free an object that was not allocated with malloc/free. –Memory leaks is less severe than the other three but is still bad.

Implicit Memory Management Let the system determine what memory is no longer in use and recycle it. Let the system determine what memory is no longer in use and recycle it. There are two basic approaches: There are two basic approaches: –Reference Counting –Tracing

Reference Counting In each object keep a counter that represents the number of references pointing to the object. In each object keep a counter that represents the number of references pointing to the object. When the reference counter reaches 0, it means that there is no reference to this object so it can be safely recycled. When the reference counter reaches 0, it means that there is no reference to this object so it can be safely recycled.

Reference Counting Advantages: Advantages: –Memory is recycled as soon as it is no longer in use. Disadvantages: Disadvantages: – cycles will not be removed because the counter never reaches 0. –Each pointer assignment will need an increment/decrement operation in the counters. –It is used by Perl, Phyton and some times in Java with auxiliar GC to collect cycles

Tracing Garbage Collection The objects in a program are divided into two groups: The objects in a program are divided into two groups: –Root objects – Objects that where not allocated with malloc/new and that may contain reference to dynamic objects. –Dynamic objects – Objects tahat are allocated with malloc/new and can become unused during the execution of the program

Tracing Garbage Collection Each object has a mark bit Each object has a mark bit A stack or queue is used to keep track of the objects that have been not been visited. A stack or queue is used to keep track of the objects that have been not been visited. Tracing Garbage Collection Algorithm Tracing Garbage Collection Algorithm –Before a GC the mark bits are cleared –Scan all the root objects for references to dynamic objects. If the object referenced has not been marked, mark it and push reference to the stack. –While the stack is not empty pop one reference from the stack, scan it for references and if the object referenced has not been marked, mark it and push reference to the stack.

Tracing Garbage Collection –Stop until the stack is empty –When the stack is empty, the unmarked objects are no longer reachable from the roots and can be safely recycled.

Tracing Garbage Collection Marked – Live Unmarked - Garbage Root Objects Dynamic Objects