Java program to find total memory space and free memory after executing garbage collector.

Below code is used to execute garbage collector and free memory in java:

import java.util.*;

public class Garbage
{
   public static void main(String[] args)
   throws Exception
   {
        Runtime rs=Runtime.getRuntime();
        System.out.println("Total memory is "+rs.totalMemory());
        System.out.println("Free memory in JVM before garbage collection = "+rs.freeMemory());
        rs.gc();
        System.out.println("Free memory in JVM after garbage collection = "+rs.freeMemory());
    }
}
Thank You.