Here you can find the source of getMemoryInfo()
public static String getMemoryInfo()
//package com.java2s; //License from project: Apache License import java.lang.management.ManagementFactory; import java.lang.management.MemoryPoolMXBean; import java.lang.management.MemoryUsage; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.*; public class Main { private static final NumberFormat MEM_FMT = new DecimalFormat("##,###.##"); public static String getMemoryInfo() { Runtime run = Runtime.getRuntime(); StringBuilder memoryInfo = new StringBuilder(); memoryInfo.append("Runtime free: ").append(kbString(run.freeMemory())).append("\nRuntime max:") .append(kbString(run.maxMemory())).append("\nRuntime total:").append(kbString(run.totalMemory())); Iterator<MemoryPoolMXBean> iter = ManagementFactory.getMemoryPoolMXBeans().iterator(); while (iter.hasNext()) { MemoryPoolMXBean item = iter.next(); MemoryUsage usage = item.getUsage(); memoryInfo.append("\nMX ").append(item.getName()).append("(").append(item.getType()).append("): ") .append("used: ").append(kbString(usage.getUsed())).append(", init: ") .append(kbString(usage.getInit())).append(", committed: ") .append(kbString(usage.getCommitted())).append(", max: ").append(kbString(usage.getMax())); }/*from w ww.ja va2 s. c om*/ return memoryInfo.toString(); } public static String kbString(long memBytes) { return MEM_FMT.format(memBytes / 1024) + " kb"; } private static String format(long bytes) { double val = bytes; int mag = 0; while (val > 1024) { val = val / 1024; mag++; } String formatted = MEM_FMT.format(val); switch (mag) { case 0: return formatted + " bytes"; case 1: return formatted + " kb"; case 2: return formatted + " Mb"; case 3: return formatted + " Gb"; default: return "WTF?"; } } }