Pages

Sunday 20 April 2014

Garbage Collection Policies

Garbage Collection is Application Programme thet starts with application server(jvm) itself and stopped when jvm comes out of its execution.

How does GC Exactly works?

GC uses mark & sweep mwthodology.
In this process it goes accross your heap and checks which memory addresses are being referenced
Those referenced addresses will be marked
Once this process is done, it collects all non-referenced objects
during this process when your application is running, let us say all of memory objects are allocated & some are deallocated of no longer it is being referenced. So, GC objects which go out of scope, you end up getting fragmentation.

How Defragmentation is done?
Defragmentation is done by garbage collector. there are 2 methods ,
1) Shift
2) Copying


%%%%%%%%%%%%%%%%%%%

GC is the best feature in java, where memory management(Automatic) is done by garbage collector.



















%%%%%%%%%%%%%%%%%%%%%%%%%%%





GC Policies
========
GC is an integral part of jvm, as it collects unused java heap memory so that the application can continue allocating new objects.

The effectiveness and performance of GC play an critical role in application performance.

The IBM JVM provided with WAS V8, provides four different GC policy algorithm.
1) -Xgcpolicy: optthruput
2) -Xgcpolicy: optavgpause
3) -Xgcpolicy: gencon
4) -Xgcpolicy: balanced

Default policy in WAS V8 was changed from -Xgcpolicy: optthruput to -Xgcpolicy: gencon .

optthruput :
``````````````
The Parallel Mark-sweep-Compact Collector

The Simplest possible GC technique is to
~ continue allocating untill free memory has been exhausted, then
~ stop the application and process entire Heap.



DIAGRAM IMAGE



The collector uses a parallel Mark-Sweep algorithm.
It means that the collector first walks through the set of reachable objects, marking them as live data.
A Second pass then sweeps away the unmarked objects, leaving behind free memory that can be used for new allocations.

The majoruty of work is done in parallel, so the collector uses additional threads, to get the job done faster, reducing the time, the application remains paused.

But, the problem with mark-sweep algorithm is that it can lead to fragmentaion
There might be lots of free memory, but if it is in small slices interspersed with live objects, then no individual peice might be large to a particular allocation.
The Solution to this is COMPACTION.
This is an expensive operation, b'coz every pointer to a moved object must be updatedto the new location.

As a results compaction is done in generally, when it is necessary.
Compaction can also be done in parallel, but it results in a less efficient packing of live objects instead of a single block of free space, several smaller ones might be created.

optavgpause:
```````````````````
The Concurrent Collector

For applications that are willing to trade some throughput for shorter pauses, a different policy is available.

The optavgpause attempts to do as much GC work possible before stopping the application, leading to shorter pauses.
The same mark-sweep-compact collector is used, but much of mark&sweep phases can be done as the application runs.
Based on the programs allocation rate, the system attempts to predict, when the next garbage collection will be required. When this threshold reaches a Concurrent GC begins.

As application threads allocate objects, they will occationally be asked to do small amount of GC work before their allocation is fulfilled.the more allocations a thread does, the more it will be asked to helpout.

Meanwhile one/more GC threads will use idle cycles to get additional work done.

Once all the concurrent work is done or if the memory is exhausted ahead of shedule, the application is halted and the collection is completed

This Pause is generally short unless a compaction is required. B'coz, Compaction requires moving and updating live objects, it can not be done concurrently.









gencon:
`````````
The Generational Collection

It has been observed that majority of objects created are used for a short period of time. These are allocated to acomplish a specific task and are rarely needed afterwards.

On a large Scale, applications that are transactional in nature also tend to create groups of objects that are used and discarded together.

This leads to generational garbage collectors.

The Idea is to devide the heap into different area and collect these areas at different rates.

New objects are allocated out of one such area called nursery (or new space)

Since most objects in this area will become garbage quickly, collecting it offers the best chance to recover memory.
once it is survived it is moved into a different area called tenured ( or oldspace). These objects are less linkely to become garbage.

IBM's gencon policy offers a generational GC("gen-") on top osf concurrent one described above("-con")

The tenured space is collected as described above while nursery space uses a copying collector.

This algorithm works by further subdividing the nursery area into allocate and survivor spaces

New objects are placed in allocate space untill free has been exhausted. The Application is themn halted and any live objects in allocate are into survivor. The two space then swap roles and the application is resumed.

If an object is survived for a number of these copies, it is moved into the tenure area instead.




No comments:

Post a Comment