List of utility methods to do Memory
long | memory() return used memory long m = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); return m; |
boolean | memoryEqual(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 { ... |
String | memoryInfo() 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)); ... |
boolean | memoryIsLow() Returns true if less then 5% of the available memory is free. return availableMemory() * 100 < RUNTIME.totalMemory() * 5;
|
long | memoryOccupied() memory Occupied Runtime runtime = Runtime.getRuntime();
return runtime.totalMemory() - runtime.freeMemory();
|
String | memorySize(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"); ... |
double | memoryUtilization() memory Utilization return 1.0 - runtime.freeMemory() / (double) runtime.maxMemory(); |
String | memoryValue(long inBytes) memory Value return byteCountValue(inBytes);
|
int | normalizeMemoryMeasure(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))); ... |
int | percentMemoryFull() 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); |