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
import java.util.*; // Start the program of garbage collection in java
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());
System.out.println("Free memory after Garbage Collection = "+rs.freeMemory());
} //End of garbage collection in java example