GARBAGE COLLECTION INTRODUCTION MARK AND SWEEP COLLECTORS COMPACTING COLLECTORS COPYING COLLECTORS GENERATIONAL COLLECTORS INCREMENTAL & CONCURRENT COLLECTORS DISCOVERING THE ROOT SET 1
1
GARBAGE COLLECTION
INTRODUCTIONMARK AND SWEEP COLLECTORSCOMPACTING COLLECTORSCOPYING COLLECTORSGENERATIONAL COLLECTORSINCREMENTAL & CONCURRENT COLLECTORSDISCOVERING THE ROOT SET
2
1. INTRODUCTION
Memory resources are not unbounded Programs may run out of Memory Objects that are no longer accessible can be collected and reused When a JVM runs out of memory, garbage collector is invoked to collect garbage
objects In JVM, a Root set of references point to objects in the Global memory heap Some objects contains references to other objects in the memory Objects that cannot be reached through this chain are Garbage objects Time spent on garbage collection is not time spent on computing There are number of methods that organize the heap differently and collects garbage
efficiently
3
4
2. MARK AND SWEEP COLLECTORS This method starts with the Root reference and traces through all reachable objects It contains stages namely,
• Marking• Sweeping
Marking• Setting of flag bit to indicate whether the object is reached or not• If already marked object is reached, tracing down that path is stopped
Sweeping• The unmarked object are considered as Garbage• These objects are combined into a linked list of free objects
DisadvantageFormation of lot of Linked lists leads to memory fragmentation and wastage
5
3. COMPACTING COLLECTORS This method slides the live objects to the bottom of the heap memory Automatically garbage objects goes up and cleanedDisadvantageIt requires number of passes1st Pass - Marking live objects2nd Pass - compute new location for live objects3rd Pass – Move objects4th Pass – Updating references Solution To use a Handle Pool
6
4. COPYING COLLECTORS This method divides heap into two halves
• Unused part• Active heap
It performs mark and sweep simultaneously
When a active object is found, it is moved to the unused part of the heap
At the end of the collection, Unused part will become active and active half becomes unused
This method trades memory space to reduce collection time
7
5. GENERATIONAL COLLECTORS Compacting Collectors and Copying Collectors move a very large fraction of live objects during every collection process
A long lived objects may be moved many times during its lifetime
This method uses a bimodal distribution
Generational Collectors attempt to group objects according to their age
Heap memory is divided into sub heaps
One sub heap holds the older objects called the tenured objects
Another heap holds newly created objects called the nursery objects
The nursery is garbage collected frequently than the tenured ones
8
6. INCREMENTAL AND CONCURRENT COLLECTORSAll of the collectors discussed before stop program execution while they perform collection and then return control to programIn Incremental Collectors, Collection time may be spread out if collection is done incrementallyIn multi Processor environment, garbage can be collected concurrently with the help of threads and program execution is not affected seriously Disadvantage
SolutionTo impose write barriers for references to objects that are already marked
9
7. DISCOVERING THE ROOT SET
The root set consists of references held in the stack and static objects called the Architected root set
In some VMs, there are memory locations that may hold root set pointers
At the time of Garbage collection, these pointers to root set will help to discover the root set easily
There are two method for discovering root sets namely,–Type accurate method–Super set method
10
THANK YOU