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

StringdescribeMemoryUsage()
Returns a string that describes the JVM's memory conditions in this format: 36M used, 29M free, 533M max .
Runtime runtime = Runtime.getRuntime();
long free = runtime.freeMemory();
return String.format("%sM used, %sM free, %sM max", (runtime.totalMemory() - free) / 1000000,
        free / 1000000, runtime.maxMemory() / 1000000);
intestimateMemoryUsage(String s)
Estimates the memory usage of the given string.
return 48 + s.length() * 2;
StringgetMemberName(int number)
get Member Name
return historyClusterMemberPrefix + memberFormat.format(number);
StringgetMemInfo()
get Mem Info
NumberFormat format = NumberFormat.getInstance(Locale.ITALIAN);
StringBuilder sb = new StringBuilder();
long allocatedMemory = runtime.totalMemory();
sb.append(format.format(allocatedMemory / 1024 / 1024)).append(" MB");
return sb.toString();
StringgetMemoryFootprint()
get Memory Footprint
Runtime runtime = Runtime.getRuntime();
String memoryInfo = "Memory - free:" + kbString(runtime.freeMemory()) + " - max:"
        + kbString(runtime.maxMemory()) + " - total:" + kbString(runtime.totalMemory());
return memoryInfo;
StringgetMemoryInfo()
get Memory Info
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();
...
StringBuffergetMemoryInfo()
get Memory Info
StringBuffer buffer = new StringBuffer();
freeMemory();
Runtime runtime = Runtime.getRuntime();
double usedMemory = usedMemory(runtime);
double maxMemory = maxMemory(runtime);
NumberFormat f = new DecimalFormat("###,##0.0");
String lineSeparator = System.getProperty("line.separator");
buffer.append("Used memory: " + f.format(usedMemory) + "MB").append(lineSeparator);
...
StringgetMemoryInfo()
get Memory Info
DecimalFormat f1 = new DecimalFormat("#,###KB");
DecimalFormat f2 = new DecimalFormat("##.#");
Runtime rt = Runtime.getRuntime();
long free = rt.freeMemory() / 1024;
long total = rt.totalMemory() / 1024;
long max = rt.maxMemory() / 1024;
long used = total - free;
double ratio = (used * 100 / (double) total);
...
StringgetMemorySize(long size)
get Memory Size
String hrSize = null;
double b = size;
double k = size / 1024.0;
double m = ((size / 1024.0) / 1024.0);
double g = (((size / 1024.0) / 1024.0) / 1024.0);
double t = ((((size / 1024.0) / 1024.0) / 1024.0) / 1024.0);
DecimalFormat dec1 = new DecimalFormat("0.00");
DecimalFormat dec2 = new DecimalFormat("0");
...
StringgetMemoryStatus()
get Memory Status
Runtime rt = Runtime.getRuntime();
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(true);
nf.setMinimumFractionDigits(0);
long total_mem = rt.totalMemory();
long free_mem = rt.freeMemory();
long used_mem = total_mem - free_mem;
return "Amount of used memory/free memory: " + nf.format(used_mem / 1000) + "KB / "
...