Garbage Collection
Garbage Collection
Introduction:
Example:
3. Objects created inside a method:
Objects created inside a method are by default eligible for GC once method
completes.
Example 1:
Example2:
Example 3:
Example 4:
4. Island of Isolation:
Internal reference is there but no external reference, that’s why the group of
object is isolated from outside.
Note: if an object doesn't have any reference then it always eligible for GC.
Note: Even though object having reference still it is eligible for GC some times.
Example:
Island of isolation: (Island of Isolation all references are internal references)
The following are various ways for requesting JVM to run GC:
By System class: System class contains a static method gc() for this purpose.
Example: System.gc();
By Runtime class:
Note: Runtime class is a singleton class so not creates the object to use constructor.
Q) Which of the following are valid ways for requesting JVM to run GC?
System.gc(); (valid) // this method is static, that’s why we are calling by using class
name.
Runtime.gc(); (invalid) // this is instance method, so we should not use class
name.
(new Runtime).gc(); (invalid)
Runtime.getRuntime().gc(); (valid)
Note:
gc() method present in System class is static, where as it is instance
method in Runtime class.
Over Runtime class gc() method , System class gc() method is
recommended to use.
In java it is not possible to find size of an object and address of an object.
Internally system class gc() method calling, runtime gc() method. So runtime
gc() method is performance wise good.
Finalization:
Case 1: Just before destroying any object GC calls finalize() method on the object
which is eligible for GC then the corresponding class finalize() method will be
executed.
For Example if String object is eligible for GC then String class finalize()method is
executed but not Test class finalize()method
In the above program String class finalize()method got executed.
Which has empty implementation.
If we replace String object with Test object then Test class finalize()
method will be executed .
Example:
class Test
{
public static void main(String args[]){
String s=new String("bhaskar");
Test t=new Test();
t=null;
System.gc();
System.out.println("End of main.");
}
public void finalize(){
System.out.println("finalize() method is executed");
}
}
Output:
finalize() method is executed
End of main
Case 2: We can call finalize() method explicitly then it will be executed just like a
normal method call and object won't be destroyed. But before destroying any
object GC always calls finalize() method.
class Test
{
public static void main(String args[]){
Test t=new Test();
t.finalize();
t.finalize();
t=null;
System.gc();
System.out.println("End of main.");
}
public void finalize(){
System.out.println("finalize() method called");
}}
Output:
finalize() method called.
finalize() method called.
finalize() method called.
End of main.
In the above program finalize() method got executed 3 times in that 2 times
explicitly by the programmer and one time by the GC.
Note: In Servlets we can call destroy() method explicitly from init() and
service() methods. Then it will be executed just like a normal method call
and Servlet object won't be destroyed.
Case 3:
Example:
If catch block is there, JVM won’t ignore. Catch block will be executed.
If catch block is not there then only JVM will ignore the exception.
Case 4: On any object GC calls finalize() method only once.
Note:
The behavior of the GC is vendor dependent and varied from JVM to JVM
hence we can't expert exact answer for the following.
Case5:
#Memory leaks:
Launch VisualVM