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

StringmemoryUsage()
Calculates the memory usage according to Runtime.
final Runtime runtime = Runtime.getRuntime();
runtime.gc();
final long max = runtime.maxMemory();
final long total = runtime.totalMemory();
final long free = runtime.freeMemory();
final long used = total - free;
return String.format("%d\t%d\t%d\t%d", max, total, free, used);
StringmemoryUsage()
memory Usage
final StringBuffer buf = new StringBuffer(80);
buf.append("(used:").append(usedMemorym());
buf.append("M/").append(freeMemorym());
buf.append("M [").append(totalMemorym());
buf.append("M,").append(maxMemorym());
buf.append("M ])");
return buf.toString();
voidprintMemoryUsage()
print Memory Usage
System.out.println(getMemoryUsageHr());
StringreportMemoryUsage()
report Memory Usage
Runtime r = Runtime.getRuntime();
return String.valueOf((r.totalMemory() - r.freeMemory()) / (1024 * 1024)) + " MB";
voidshowMemoryUsage()
show Memory Usage
Runtime runtime = Runtime.getRuntime();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
System.out.println("-- MEMORY USAGE -----------------");
System.out.println("-- free memory: " + freeMemory / 1024);
System.out.println("-- allocated memory: " + allocatedMemory / 1024);
System.out.println("-- max memory: " + maxMemory / 1024);
...