The java run-time JVM deletes the created objects when it determines that they are no longer being used. This is called as garbage collection in java. In java we can create as many you want and you don’t have to worry about to destroying them. The object is eligible for java garbage collection where there are no more references to that object. So when we call gc() method then it shows the free memory of garbage collection java. In the secondary memory there is lots of wasted memory occupied by some other objects which is display through the java garbage collection.
As the same find out the previous and post for garbage collection in java. The below program is for garbage collection in java example.
Program for java garbage collection with Example
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.*; // Start the program of garbage collection in java class GarbageCollection { public static void main(String s[]) throws Exception { Runtime rs = Runtime.getRuntime(); //Logic for java garbage collection System.out.println("Free memory before Garbage Collection = "+rs.freeMemory()); rs.gc(); System.out.println("Free memory after Garbage Collection = "+rs.freeMemory()); } } //End of garbage collection in java example |