Tuesday, February 7, 2017

Garbage Collector and its Algorithms - JAVA

-------------------Garbage Collection-----------------------
In java , programmer need not to care about the objects which are no longer in use. garbage collector
destroy those objects and make the memory available for the programmer to use again.
- garbage collector not guaranteed to run at any specific time.
there are few method of garbage collector



------------The finalize () method:
Called by the garbage collector on an object when garbage collector determines that there are no more references to the object
Note :

The finalize method is never invoked more than once by a Java virtual machine for any given object.
Our program must not rely on the finalize method because we never know if finalize will be executed or not.

----------------gc() – request to JVM

We can request to run the garbage collector using java.lang.System.gc() but it does not force garbage collection, the JVM will run garbage collection only when it wants to run it.­­
We may use system.gc() or runtime.gc()
import java.lang.*;
public class Test
{
    public static void main(String[] args)
    {
        int g1[] = { 0, 1, 2, 3, 4, 5 };
        System.out.println(g1[1] + " ");

        // Requesting Garbage Collector
        System.gc();
        System.out.println("Hey I just requested "+
                          "for Garbage Collection");
    }
}

--------What happens when group of object only refer to each other?

It is possible that a set of unused objects only refer to each other.
This is also referred as Island of Isolation.
For example, object o1 refers to object o2. Object o2 refers to o1. None of them is referenced by any other object.
In this case both the objects o1 and o2 are eligible for garbage collection.


-------------------ALGORITHM USED BY Garbage Collector to remove objects.
Garbage Collector uses 2 alogrithm to remove objects

* Mark and sweep Algorithm
* Island of isolation


-------------------Mark and sweep alogrithm-------------
The main activity of garbage collector is to first identify the unused objects and them claim the head memory to available for use.
There are 2 phase to perform in marks and sweep alogrithm-
1) mark phase
2) sweep phase

In Mark Phase , whenever any object is created its mark bit is set to be false (0) , so in mark phase we set all the reachable object marked bit as true(1),
to perfome is operation , we simply do graph traversal , deepth first search in particular, here we consider all objects as nodes ,and all the nodes connected to one nodes are visited and set as true.

Root is a variable that refer to an object and is directly accessible by local variable. We will assume that we have one root only.
We can access the mark bit for an object by: markedBit(obj).

In Sweep Phase : In sweep phase , all the unreachable objects are sweeped , i.e all the objects whose marked bits are false , will be remove heap memory.
and then the marked bit of all the reachable object will again sets as false , so that they will be again available to recurring process.


------------Island of Isolation -----------------------

Object 1 references Object 2 and Object 2 references Object 1. Neither Object 1 nor Object 2 is referenced by any other object. That’s an island of isolation.
Basically, an island of isolation is a group of objects that reference each other but they are not referenced by any active object in the application. Strictly speaking, even a single unreferenced object is an island of isolation too.

0 comments:

Post a Comment