Java Utililty Methods Memory Usage

List of utility methods to do Memory Usage

Description

The list of methods to do Memory Usage are organized into topic(s).

Method

longgetMemoryUsage()
get Memory Usage
Runtime runtime = Runtime.getRuntime();
long memoryInUse = runtime.totalMemory();
return memoryInUse;
doublegetMemoryUsage()
get Memory Usage
long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
return (double) usedMemory / (double) MEGABYTE_SIZE;
StringgetMemoryUsageSimpleHr()
get Memory Usage Simple Hr
return getHumanReadableByteCount(getMemoryUsed()) + " / " + getHumanReadableByteCount(getMemoryMax());
StringgetMemoryUsageUnix()
get Memory Usage Unix
return "Not supported yet."; 
StringgetMemoryUtiliztaion()
get Memory Utiliztaion
Runtime runtime = Runtime.getRuntime();
return "heap=" + df.format(runtime.totalMemory()) + " max=" + df.format(runtime.maxMemory()) + " used="
        + df.format(runtime.totalMemory() - runtime.freeMemory()) + " free="
        + df.format(runtime.freeMemory());
StringgetMemUsage()
get Mem Usage
if (mbean == null) {
    mbean = ManagementFactory.getMemoryMXBean();
long used = mbean.getHeapMemoryUsage().getUsed();
long max = mbean.getHeapMemoryUsage().getMax();
NumberFormat f = DecimalFormat.getInstance();
f.setMaximumFractionDigits(2);
f.setMinimumFractionDigits(2);
...
doublegetPercentMemoryUsage()
get Percent Memory Usage
return ((double) getMemoryUsage() / (double) getMaxMemory() * 100);
intgetStringMemoryUsage(String s)
Calculates the memory used by a string's data (not including the String object itself).
return s.length() * 2;
longgetUsageMemory()
get Usage Memory
return getAllocatedMemory() - getFreeMemory();
StringmemoryUsage()
memory Usage
Runtime r = Runtime.getRuntime();
long used = r.totalMemory() - r.freeMemory();
return "usedHeap=" + (used / 1000000) + "MB, percent=" + (100.0 * used / r.maxMemory());