Java Program for Memory

Here is a simple java program for memory…

import java.io.*;
import java.lang.*;
class Memory
   {
      public static void main(String args[]) throws IOException
     {
Runtime r=Runtime.getRuntime();
long mem1=0,mem2=0;
Integer someints[]=new Integer[1000];
System.out.println(“The total memory is = ” + r.totalMemory());
mem1=r.freeMemory();
System.out.println(“The initial free memory is = ” + mem1);
r.gc();
mem1=r.freeMemory();
System.out.println(“the free memory after garbage collector is = ” + mem1);
for(int i=0;i<1000;i++)
someints[i]=new Integer(i);
mem2=r.freeMemory();
System.out.println(“free memory after allocation is = ” + mem2);
System.out.println(“memory used by allocation” + (mem1-mem2));
for(int i=0;i<1000;i++)
someints[i]=null;
r.gc();
mem2=r.freeMemory();
System.out.println(“free memory after discarded integers and allocation ” + mem2);
        }
  }

Add Comment