Java Memory Total getTotalMemory(boolean preRunGarbageCollector)

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

Description

get Total Memory

License

Open Source License

Declaration

public static long getTotalMemory(boolean preRunGarbageCollector) 

Method Source Code

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

public class Main {
    public static long getTotalMemory(boolean preRunGarbageCollector) {
        Runtime rt = getRuntimeAndRunGC(preRunGarbageCollector);
        return bytesToMegabytes(rt.totalMemory());
    }//from  w ww.  j  av a  2s . c  o m

    public static long getTotalMemory() {
        return bytesToMegabytes(Runtime.getRuntime().totalMemory());
    }

    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. getJvmTotalMemory()
  2. getMemoryTotal()
  3. getTotalFreeMemory()
  4. getTotalMemory()
  5. getTotalMemory()
  6. getTotalMemoryInKB()
  7. getTotalMemoryInMb()
  8. jvmTotalMemory(String size, Boolean txtByte)
  9. memoryTotal()