Monday, August 17, 2015

Memory management in JAVA

Sometimes when you run some java applications you may run out of memory and get exceptions related to memory.

Following code helps you to manage the memory better. Also you may trigger an email to the users or dvelopment team whenever you anticipate shortage of space, so that the development team can take necessary action.

public class MemoryUsage
{

  public static void main(final String[] args)
  {
   final int mb = 1024 * 1024;

   final Runtime runIns = Runtime.getRuntime();
   
   System.out.println("Total Memory = " + runIns.totalMemory() / mb + " mb");
  
   System.out.println("Free Memory = " + runIns.freeMemory() / mb + " mb");
   
   System.out.println("Used Memory = " + (runIns.totalMemory() - runIns.freeMemory()) / mb + " mb");
  
   System.out.println("Max Memory= " + runIns.maxMemory() / mb + " mb");
  }

}

No comments:

Post a Comment

Home