Java program to perform garbage collection


-

-

Example:

Copied
import java.util.*;
class JavaGarbage {
	public static void main(String s[]) throws Exception {
		Runtime x =  Runtime.getRuntime();
		System.out.println("Free Memory in JVM before Garbage Collection: "+x.freeMemory());
		x.gc();
		System.out.println("Free Memory in JVM after Garbage Collection: "+x.freeMemory());
	}
}

Output:

Free Memory in JVM before Garbage Collection: 13503432
Free Memory in JVM after Garbage Collection: 13756480