Java Utililty Methods Memory Free

List of utility methods to do Memory Free

Description

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

Method

booleanbelowMemoryLimit(int percentFree)
below Memory Limit
long max = Runtime.getRuntime().maxMemory() / 1000;
long free = freeMemory();
if ((double) free / max * 100 < percentFree)
    return true;
return false;
booleancheckFreeMemory(long requiredMemory)
Check if there is enough free memory.
if (Runtime.getRuntime().freeMemory() >= requiredMemory) {
    return true;
return false;
StringformatMemorySize(Long afreeDiskSpace)
format Memory Size
freeDiskSpace = afreeDiskSpace;
if (afreeDiskSpace < 1024) {
    return "Free Disk Space: " + afreeDiskSpace + "B";
} else if (afreeDiskSpace < 1048576) {
    return "Free Disk Space: " + afreeDiskSpace / 1024 + "KB";
} else if (afreeDiskSpace < 1073741824) {
    return "Free Disk Space: " + afreeDiskSpace / 1048576 + "MB";
} else {
...
longfreeAllocatedMemory()
Returns the amount of free memory in the Java Virtual Machine.
return Runtime.getRuntime().freeMemory();
longfreeMemory()
free Memory
return Runtime.getRuntime().freeMemory();
voidfreeMemory()
Utility to free memory
long total = Runtime.getRuntime().totalMemory();
for (int i = 0; i < 10; i++) {
    System.gc();
while (true) {
    System.gc();
    long newtotal = Runtime.getRuntime().totalMemory();
    if ((((double) (total - newtotal)) / (double) total) < 0.1) {
...
voidfreeMemory()
free Memory
try {
    System.gc();
    System.gc();
    Thread.currentThread().sleep(250);
    System.runFinalization();
    System.runFinalization();
    Thread.currentThread().sleep(250);
    System.gc();
...
longfreeMemory()
Wrapper around Runtime.getRuntime().freeMemory()
return Runtime.getRuntime().freeMemory();
longfreeMemory()
free Memory
return runtime.freeMemory();
intfreeMemorym()
free Memorym
return (int) Math.floor((freeMemoryb() / megabytes));