Java Utililty Methods Memory

List of utility methods to do Memory

Description

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

Method

longmemory()
return used memory
long m = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
return m;
booleanmemoryEqual(byte[] left, int leftOffset, byte[] right, int rightOffset, int length)
Description: Checks Whether the Two Parts of Arrays are Equal to Each Other
if ((leftOffset + length <= left.length) && (rightOffset + length <= right.length)) {
    for (int i = 0; i < length; i++) {
        if (left[leftOffset + i] != right[rightOffset + i]) {
            return false;
    return true;
} else {
...
StringmemoryInfo()
memory Info
Runtime runtime = Runtime.getRuntime();
StringBuilder sb = new StringBuilder();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
sb.append("heap: ").append(format(allocatedMemory - freeMemory));
sb.append(", allocated: ").append(format(allocatedMemory));
sb.append(", free: ").append(format(freeMemory));
...
booleanmemoryIsLow()
Returns true if less then 5% of the available memory is free.
return availableMemory() * 100 < RUNTIME.totalMemory() * 5;
longmemoryOccupied()
memory Occupied
Runtime runtime = Runtime.getRuntime();
return runtime.totalMemory() - runtime.freeMemory();
StringmemorySize(long bytesSize)
memory Size
if (bytesSize < 1024) {
    return stringBuffer(bytesSize, 'B');
} else if (bytesSize < 1024 * 1024) {
    float kb = (float) bytesSize / 1024.f;
    return stringBuffer(String.format("%.2f", kb), "KB");
} else {
    float mb = (float) bytesSize / 1024.f / 1024.f;
    return stringBuffer(String.format("%.2f", mb), "MB");
...
doublememoryUtilization()
memory Utilization
return 1.0 - runtime.freeMemory() / (double) runtime.maxMemory();
StringmemoryValue(long inBytes)
memory Value
return byteCountValue(inBytes);
intnormalizeMemoryMeasure(String memory)
Normalizes the string memory measurement to a MB integer value.
if (memory.toLowerCase().endsWith("m")) 
    return Integer.parseInt(memory.substring(0, memory.length() - 1));
if (memory.toLowerCase().endsWith("mb")) 
    return Integer.parseInt(memory.substring(0, memory.length() - 2));
if (memory.toLowerCase().endsWith("g")) 
    return (1024 * Integer.parseInt(memory.substring(0, memory.length() - 1)));
if (memory.toLowerCase().endsWith("gb")) 
    return (1024 * Integer.parseInt(memory.substring(0, memory.length() - 2)));
...
intpercentMemoryFull()
percent Memory Full
System.gc();
Runtime r = Runtime.getRuntime();
double free = r.freeMemory();
double total = r.totalMemory();
double used = total - free;
double percent = used / total;
return (int) (percent * 100.0);