Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark and Sweep Algorithm Reference Counting Memory Related PitFalls

Similar presentations


Presentation on theme: "Mark and Sweep Algorithm Reference Counting Memory Related PitFalls"— Presentation transcript:

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

2 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

3 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.

4 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.

5 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.

6 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)

7 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

8 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

9 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); }

10 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

11 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

12 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

13 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.

14 Memory Related Pitfalls

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

16 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; }

17 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)); }

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

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


Download ppt "Mark and Sweep Algorithm Reference Counting Memory Related PitFalls"

Similar presentations


Ads by Google