Garbage Collection

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

Garbage Collection

The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. A garbage collector automatically searches out garbage and reallocates it Practically every modern language, except C/C++, uses a garbage collector

What is Garbage Collection?


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.

Garbage Collection
What is garbage and how can we deal with it?

Garbage collection schemes


Reference Counting Mark and Sweep Stop and Copy

Advantages of Garbage Collection


GC eliminates the need for the programmer to deallocate memory blocks explicitly . 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.

Common Garbage Collection Schemes


Three main methods of garbage collection:
Reference counting

Mark-and-sweep
Stop-and-copy garbage collection.

Reference Counting Garbage Collection

Count for each object. Count increases as number of reference increases. Eligible for GC when count equals zero. Disadvantages Overhead of incrementing and decrementing reference count.

Mark-and-Sweep Garbage Collection


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

Stop-and-Copy Garbage Collection


The heap is divided into two regions: Active and Inactive. Objects are allocated from the active region only. When all the space in the active region has been exhausted, program execution is stopped and the heap is traversed. Live objects are copied to the other region as they are encountered by the traversal. The role of the two regions is reversed, i.e., swap (active, inactive).

Stop-and-Copy Garbage Collection (cont'd)


A graphical depiction of a garbage-collected heap that uses a stop and copy algorithm. This figure shows nine snapshots of the heap over time:

Generational Collectors
Based on lifetimes of objects. Short lived objects Long lived objects Collection of short lived objects done more often. Heap is divided into two or more sub heaps Objects are promoted to different heaps based on survival.

You might also like