
class MemoryTest
{
	public static void main(String args[]) 
	{
		Runtime r = Runtime.getRuntime();
	    long initMem1, afterMem2;
		Integer iVar[] = new Integer[1000];

		System.out.println("Total memory is     : "+ r.totalMemory());
	    initMem1 = r.freeMemory();

		System.out.println("Initial free memory : " + initMem1);
		//r.gc();
		
		initMem1 = r.freeMemory();
		System.out.println("Free memory after garbage collection : "+ initMem1);

		for(int i=0; i<1000; i++)
		      iVar[i] = new Integer(i); // allocate integers

		afterMem2 = r.freeMemory();
		System.out.println("Free memory after allocation  : " + afterMem2);
		System.out.println("Memory used by allocation  :  "    +(initMem1-afterMem2));

		// discard Integers
		for(int i=0; i<1000; i++) iVar[i] = null;

		//r.gc(); // request garbage collection
		afterMem2 = r.freeMemory();
		System.out.println("Free memory after collecting" +" discarded Integers: " + afterMem2);
	}
}