Java Memory Used getMemoryUse()

Here you can find the source of getMemoryUse()

Description

retrieve the usage of memory.

License

Apache License

Return

the size of memory has been used

Declaration

public static long getMemoryUse() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static long fSLEEP_INTERVAL = 100;

    /**/*from w  w  w  .ja  v  a2 s. c o  m*/
     * retrieve the usage of memory.
     * 
     * @return the size of memory has been used
     */
    public static long getMemoryUse() {
        putOutTheGarbage();
        long totalMemory = Runtime.getRuntime().totalMemory();
        putOutTheGarbage();
        long freeMemory = Runtime.getRuntime().freeMemory();
        return (totalMemory - freeMemory);
    }

    /**
     * run garbage collections.
     */
    private static void putOutTheGarbage() {
        collectGarbage();
        collectGarbage();
    }

    /**
     * run a garbage collection.
     */
    public static void collectGarbage() {
        try {
            System.gc();
            Thread.sleep(fSLEEP_INTERVAL);
            System.runFinalization();
            Thread.sleep(fSLEEP_INTERVAL);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

Related

  1. calcUsedMemorySize()
  2. disposeUnusedMemory()
  3. getAvailableUnusedMemory()
  4. getMemoryInUse()
  5. getMemoryUsed()
  6. getMemoryUsed()
  7. getMemoryUsed()
  8. getMemoryUsedBytes ()