Java Memory Free getFreeMemory(boolean preRunGarbageCollector)

Here you can find the source of getFreeMemory(boolean preRunGarbageCollector)

Description

get Free Memory

License

Open Source License

Declaration

public static long getFreeMemory(boolean preRunGarbageCollector) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static long getFreeMemory(boolean preRunGarbageCollector) {
        Runtime rt = getRuntimeAndRunGC(preRunGarbageCollector);
        return bytesToMegabytes(rt.freeMemory());
    }/*ww  w . j  a  v a 2s  .co m*/

    private static Runtime getRuntimeAndRunGC(boolean preRunGarbageCollector) {
        Runtime rt = Runtime.getRuntime();
        if (preRunGarbageCollector) {
            //long startTime = System.currentTimeMillis();                               //DEBUG
            rt.gc();
            //System.out.println("GC takes "+(System.currentTimeMillis()-startTime)+" ms.");   //DEBUG
        }
        return rt;
    }

    private static long bytesToMegabytes(long bytes) {
        long MEGABYTE = 1024L * 1024L;
        return bytes / MEGABYTE;
    }
}

Related

  1. getFreeJVMMemoryMB()
  2. getFreeLiveMemory()
  3. getFreeMemory()
  4. getFreeMemory()
  5. getFreeMemory()
  6. getFreeMemoryForAppInKB()
  7. getFreeMemoryInMb()
  8. getFreeMemoryKb()
  9. getFreeMemoryMB()