Garbage Collection:
Introduction:
The ways to make an object eligible for GC:
The following are various possible ways to make an object eligible for GC:
![]()
![]()
![]() Example 2: ![]() Example 3: ![]() Example 4: ![]() 4.Island of Isolation:![]()
Note: if an object doesn't have any reference then it always eligible for GC. Example: island of isolation. (Island of Isolation all references are internal references ) The methods for requesting JVM to run GC:
The following are various ways for requesting jvm to run GC:
Runtime r=Runtime.getRuntime(); Once we got Runtime object we can call the following methods on that object.
freeMemory(): returns the free memory present in the heap. import java.util.Date; class RuntimeDemo { public static void main(String args[]){ Runtime r=Runtime.getRuntime(); System.out.println("total memory of the heap :"+r.totalMemory()); System.out.println("free memory of the heap :"+r.freeMemory()); for(int i=0;i<10000;i++) { Date d=new Date(); d=null; } System.out.println("free memory of the heap :"+r.freeMemory()); r.gc(); System.out.println("free memory of the heap :"+r.freeMemory()); } } Output: Total memory of the heap: 5177344 Free memory of the heap: 4994920 Free memory of the heap: 4743408 Free memory of the heap: 5049776Note : Runtime class is a singleton class so not create the object to use constructor.
Which of the following are valid ways for requesting jvm to run GC ?
Note: gc() method present in System class is static, where as it is instance method in Runtime class. Finalization:
protected void finalize() throws Throwable 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. Example:class Test { public static void main(String args[]){ String s=new String("bhaskar"); Test t=new Test(); s=null; System.gc(); System.out.println("End of main."); } public void finalize(){ System.out.println("finalize() method is executed"); } } Output: End of main.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 . The following program is an Example of this. 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. Example: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. ![]() Case 3:
finalize() method can be call either by the programmer or by the GC . If the programmer calls explicitly finalize() method and while executing the finalize() method if an exception raised and uncaught then the program will be terminated abnormally. class Test { public static void main(String args[]){ Test t=new Test(); //t.finalize();-------line(1) t=null; System.gc(); System.out.println("End of main."); } public void finalize(){ System.out.println("finalize() method called"); System.out.println(10/0); }
If we are not comment line1 then programmer calling finalize() method explicitly and while executing the finalize()method ArithmeticException raised which is uncaught hence the program terminated abnormally.
Which of the following is true? Case 4:On any object GC calls finalize() method only once. Example:class FinalizeDemo { static FinalizeDemo s; public static void main(String args[])throws Exception{ FinalizeDemo f=new FinalizeDemo(); System.out.println(f.hashCode()); f=null; System.gc(); Thread.sleep(5000); System.out.println(s.hashCode()); s=null; System.gc(); Thread.sleep(5000); System.out.println("end of main method"); } public void finalize() { System.out.println("finalize method called"); s=this; } } Output: D:\Enum>java FinalizeDemo 4072869 finalize method called 4072869 End of main methodNote: The behavior of the GC is vendor dependent and varied from JVM to JVM hence we can't expert exact answer for the following.
When ever the program runs with low memory then the JVM runs GC, but we can't except exactly at what time.
Memory leaks:
Example: HPJ meter HP ovo IBM Tivoli These are monitoring tools. J Probe (or memory management tools) Patrol and etc |