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

voidremoveSliceFromMemory(String schemaName, String cubeName, String loadName)
remove Slice From Memory
voidreportMemory()
report Memory
Runtime runtime = Runtime.getRuntime();
long mb = 1024 * 1024;
long used = runtime.totalMemory() - runtime.freeMemory();
System.out.printf("......................... memory in use: %d MB .........................%n", used / mb);
StringreportMemory(double val)
report Memory
val /= 1024;
return String.format(" %.4f KB | %.4f MB | %.4f GB", val, val / 1024, val / 1024 / 1024);
StringsimpleMemory()
Return the memory in a simple format.
StringBuilder memory = new StringBuilder();
Runtime runtime = Runtime.getRuntime();
long maxMemory = runtime.maxMemory();
long totalMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
long usedMemory = totalMemory - freeMemory;
memory.append(toSimpleBytes(usedMemory));
memory.append("/");
...
booleantestMemory(long bytesToTest)
test Memory
System.gc();
if (System.getProperties().getProperty("os.name").startsWith("Windows")) {
    Runtime r = Runtime.getRuntime();
    long avail = r.maxMemory() - r.totalMemory() + r.freeMemory();
    return (avail >= bytesToTest);
int MEGABYTE = 1048576;
int maximumMeg = (int) Math.ceil(bytesToTest / (double) MEGABYTE);
...
voidthrowOutOfMemoryError()
throw Out Of Memory Error
throw new OutOfMemoryError("java");
voidunlimitMemory()
unlimit Memory
try {
    String[] cmd = { "/bin/sh", "-c", "ulimit -s unlimited" };
    Process proc = Runtime.getRuntime().exec(cmd);
    proc.waitFor();
} catch (Exception e) {
    System.err.println("Wrong");
voidwaitForMemory(double memory)
Wait till the given requested amount of MegaBytes is free in memory
long memoryInBytes = ConvertMegaByteToByte(memory);
while (Runtime.getRuntime().freeMemory() < memoryInBytes) {
    System.gc();
    try {
        Thread.sleep(500);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
voidwriteMemory()
write Memory
Runtime rt = Runtime.getRuntime();
System.out.println("Total " + rt.totalMemory() + "\tMax " + rt.maxMemory() + "\tFree " + rt.freeMemory());