Here you can find the source of getMemoryUsage()
public static String getMemoryUsage()
//package com.java2s; //License from project: Apache License public class Main { public static String getMemoryUsage() { String result = "free: " + format(Runtime.getRuntime().freeMemory()); result += ", max: " + format(Runtime.getRuntime().maxMemory()); return result; }//w w w. ja v a 2 s .c o m private static String format(long mem) { double dMem = mem; String[] units = new String[] { "", "KB", "MB", "GB" }; for (String unit : units) { if (dMem < 1024) return String.format("%.1f %s", dMem, unit); dMem /= 1024; } return String.format("%.1f %s", dMem * 1024, units[units.length - 1]); } }