Gadgets

Thursday, 18 February 2016

Garbage collection interview questions and answers

Garbage Collector is used to destroy the useless objects. Generally java programmer is responsible to create object but programmer is no need to delete the object after usage. JVM will call the Garbage collector and it is responsible to delete that object.
1) What is the Garbage Collector?
Garbage Collector is used to destroy the useless objects.

2) What is the finalize() method?
finalize() method is called by the garbage collector before destroying the object to perform the cleanup activities of destroy object.
finalize() method is available in Object class.

3) How many time garbage collector calls finalize() method?
Garbage collector calls finalize() method only once even that object eligible for garbage collector multiple times.

4) What is the System.gc()?
System.gc() is used to request the JVM to run garbage collector.
Garbage Collector is not destroy the useless objects directly, whenever JVM call garbage collector then only the object will be destroy.

6) What is the Runtime class?
By using Runtime class we can calculate the free memory and total memory of heap area. We can create Runtime object by using getRuntime() method.
Runtime class is a singleton class.
Runtime r=Runtime.getRuntime();
         r.totalMemory();
         r.freeMemory();

7) What is singleton class?
A java class which is allowed to create only one object is called singleton class. The main advantage is if several people having same requirement then for every requirement creating a separate object is not recommended. We can use singleton object for every requirement so that performance and memory utilization will be improve.

8) Is it possible to call finalize() method explicitly by the programmer.
We can call finalize() method explicitly but it will execute just like normal method and it won’t destroy the objects.

9) What are the various ways to call garbage collector?
We can call the garbage collector by using System.gc() and Runtime.getRuntime.gc()

10) What is island of isolation?
If all objects references are internally have references is called island of isolation

class Test
{
            Test name;
            public static void main(String[] args)
            {
                        Test obj1=new Test();
                        Test obj2=new Test();
                        Test obj3=new Test();
                        obj1.name=obj2;
                        obj2.name=obj3;
                        obj3.name=obj1;
            }
}

island of isolation

No comments:

Post a Comment