Mark and Sweep Algorithm Reference Counting Memory Related PitFalls

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

Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.
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.
Instructors: Seth Copen Goldstein, Franz Franchetti, Greg Kesden
1 Binghamton University Dynamic Memory Allocation: Advanced Concepts CS220: Computer Systems II.
Dynamic Memory Allocation Topics Simple explicit allocators Data structures Mechanisms Policies.
Some topics in Memory Management
University of Washington Today Dynamic memory allocation  Size of data structures may only be known at run time  Need to allocate space on the heap 
1 Dynamic Memory Allocation: Advanced Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron Joke of the day: Why did the.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
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.
Carnegie Mellon 1 Dynamic Memory Allocation: Advanced Concepts : Introduction to Computer Systems 18 th Lecture, Oct. 26, 2010 Instructors: Randy.
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 31 st Instructors: Gregory Kesden and Markus Püschel.
Dynamic Memory Allocation II Nov 7, 2002 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Memory-related perils and pitfalls.
– 1 – Dynamic Memory Allocation – beyond the stack and globals Stack Easy to allocate (decrement esp) Easy to deallocate (increment esp) Automatic allocation.
Dynamic Memory Allocation I May 21, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies.
Pointers and Memory Allocation – part 2 -L. Grewe.
Dynamic Memory Allocation II November 7, 2007 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Review of pointers Memory-related.
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.
Dynamic Memory Allocation II March 27, 2008 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Review of pointers Memory-related.
University of Washington Memory Allocation III The Hardware/Software Interface CSE351 Winter 2013.
Explicit Free Lists Use data space for link pointers –Typically doubly linked –Still need boundary tags for coalescing –It is important to realize that.
Memory Management II: Dynamic Storage Allocation Mar 7, 2000 Topics Segregated free lists –Buddy system Garbage collection –Mark and Sweep –Copying –Reference.
Dynamic Memory Allocation II
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
Dynamic Memory Allocation II Topics Explicit doubly-linked free lists Segregated free lists Garbage collection.
Dynamic Memory Allocation II April 1, 2004 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Memory-related perils and.
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)
Dynamic Memory Allocation II November 4, 2004 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Memory-related perils and.
Today Moving on to… Memory allocation.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
University of Washington Today More memory allocation!
1 Advanced Topics. 2 Outline Garbage collection Memory-related perils and pitfalls Suggested reading: 9.10, 9.11.
Dynamic Memory Allocation II March 27, 2008 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Review of pointers Memory-related.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation: Advanced Concepts :
Memory-Related Perils and Pitfalls in C
Instructor: Fatma CORUT ERGİN
Garbage Collection What is garbage and how can we deal with it?
Dynamic Memory Allocation II
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Instructors: Roger Dannenberg and Greg Ganger
Memory Allocation III CSE 351 Spring 2017
Dynamic Memory Allocation II Nov 7, 2000
Memory Management: Dynamic Allocation
Concepts of programming languages
Memory Allocation III CSE 351 Autumn 2017
Dynamic Memory Allocation – beyond the stack and globals
Dynamic Memory Allocation: Advanced Concepts /18-213/14-513/15-513: Introduction to Computer Systems 20th Lecture, November 2, 2017.
Memory Allocation III CSE 351 Autumn 2016
Memory Management III: Perils and pitfalls Mar 13, 2001
Strategies for automatic memory management
Memory Management III: Perils and pitfalls Mar 9, 2000
Memory Allocation III CSE 351 Summer 2018
Pointer & Memory Allocation Review
Memory Allocation III CSE 351 Spring 2017
CS703 - Advanced Operating Systems
Memory Allocation III CSE 351 Spring 2017
Memory Allocation III CSE 351 Autumn 2018
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Mark and Sweep Algorithm Reference Counting Memory Related PitFalls Garbage Collection Mark and Sweep Algorithm Reference Counting Memory Related PitFalls

What is Garbage? Ali Object Garbage: unreferenced objects Student ali= new Student(); Student khalid= new Student(); ali=khalid; Now ali Object becomes a garbage, It is unreferenced Object ail khalid void foo() { int *p = malloc(128); return; /* p block is now garbage */ } Khalid Object

What is Garbage Collection? Finding garbage and reclaiming memory allocated to it. Why Garbage Collection? the heap space occupied by an un-referenced object can be recycled and made available for subsequent new objects When is the Garbage Collection process invoked? When the total memory allocated to a Java program exceeds some threshold. Is a running program affected by garbage collection? Yes, the program suspends during garbage collection.

Advantages of Garbage Collection GC eliminates the need for the programmer to deallocate memory blocks explicitly Garbage collection helps ensure program integrity. Garbage collection can also dramatically simplify programs.

Disadvantages of Garbage Collection Garbage collection adds an overhead that can affect program performance. GC requires extra memory. Programmers have less control over the scheduling of CPU time.

Memory as a Graph We view memory as a directed graph Each block is a node in the graph Each pointer is an edge in the graph Locations not in the heap that contain pointers into the heap are called root nodes (e.g. registers, locations on the stack, global variables) Root nodes Heap nodes reachable Not-reachable (garbage) A node (block) is reachable if there is a path from any root to that node. Non-reachable nodes are garbage(cannot be needed by the application)

Mark and Sweep Collecting Can build on top of malloc/free package Allocate using malloc until you “run out of space” When out of space: Use extra mark bitin the head of each block Mark:Start at roots and set mark bit on each reachable block Sweep:Scan all blocks and free blocks that are not marked root Before mark Note: arrows here denote memory refs, not free list ptrs. After mark Mark bit set After sweep free

Assumptions For a Simple Implementation Application new(n): returns pointer to new block with all locations cleared read(b,i):read location i of block b into register write(b,i,v): write v into location i of block b Each block will have a header word addressed as b[-1], for a block b Used for different purposes in different collectors Instructions used by the Garbage Collector is_ptr(p): determines whether p is a pointer length(b): returns the length of block b, not including the header get_roots(): returns all the roots

Mark and Sweep (cont.) Mark using depth-first traversal of the memory graph ptr mark(ptr p) { if (!is_ptr(p)) return; // do nothing if not pointer if (markBitSet(p)) return; // check if already marked setMarkBit(p); // set the mark bit for (i=0; i< length(p); i++) // call mark on all words mark(p[i]); // in the block return; } Sweep using lengths to find next block ptr sweep(ptr p, ptr end) { while (p < end) { if markBitSet(p) clearMarkBit(); else if (allocateBitSet(p)) free(p); p += length(p); }

Reference Counting Garbage Collection Main Idea: Add a reference count field for every object. This Field is updated when the number of references to an object changes. Example Object p= new Integer(57); Object q = p; p 57 refCount = 2 q

Reference Counting (cont'd) The update of reference field when we have a reference assignment ( i.e p=q) can be implemented as follows if (p!=q) { if (p!=null) --p.refCount; p=q; ++p.refCount; } Example: Object p = new Integer(57); Object q= new Integer(99); p=q p 57 refCount = 0 q 99 refCount = 2

Reference Counting (cont'd) Reference counting will fail whenever the data structure contains a cycle of references and the cycle is not reachable from a global or local reference ListElements ListElements ListElements head next next next refCount = 1 refCount = 1 refCount = 1

Reference Counting (cont'd) Advantages Conceptually simple: Garbage is easily identified It is easy to implement. Immediate reclamation of storage Disadvantages Reference counting does not detect garbage with cyclic references. The overhead of incrementing and decrementing the reference count each time. Extra space: A count field is needed in each object. It may increase heap fragmentation.

Memory Related Pitfalls

Dereferencing Bad Pointers The classic scanf bug intval; ... scanf(“%d”, val);

Reading Uninitialized Memory Assuming that heap data is initialized to zero /* return y = Ax */ int *matvec(int **A, int *x) { int *y = malloc(N*sizeof(int)); inti, j; for (i=0; i<N; i++) for (j=0; j<N; j++) y[i] += A[i][j]*x[j]; return y; }

Overwriting Memory Allocating the (possibly) wrong sized object int **p; p = malloc(N*sizeof(int)); for (i=0; i<N; i++) { p[i] = malloc(M*sizeof(int)); }

Overwriting Memory Not checking the max string size Basis for classic buffer overflow attacks char s[8]; inti; gets(s); /* reads “123456789” from stdin */

Failing to Free Blocks (Memory Leaks) Slow, long-term killer! foo() { int *x = malloc(N*sizeof(int)); ... return; }