Java Memory Usage getMemoryUsage()

Here you can find the source of getMemoryUsage()

Description

get Memory Usage

License

Apache License

Declaration

public static long getMemoryUsage() 

Method Source Code

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

public class Main {
    public static long getMemoryUsage() {
        takeOutGarbage();/* www  .ja va2s.  c om*/
        long totalMemory = Runtime.getRuntime().totalMemory();

        takeOutGarbage();
        long freeMemory = Runtime.getRuntime().freeMemory();

        return (totalMemory - freeMemory);
    }

    private static void takeOutGarbage() {
        collectGarbage();
        collectGarbage();
    }

    private static void collectGarbage() {
        try {
            System.gc();
            Thread.currentThread().sleep(100);
            System.runFinalization();
            Thread.currentThread().sleep(100);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Related

  1. getMemoryUsage()
  2. getMemoryUsage()
  3. getMemoryUsage()
  4. getMemoryUsage()
  5. getMemoryUsage()
  6. getMemoryUsage()
  7. getMemoryUsage()
  8. getMemoryUsage()
  9. getMemoryUsage()