Java Memory Usage getMemoryUsage()

Here you can find the source of getMemoryUsage()

Description

get Memory Usage

License

Apache License

Declaration

public static String getMemoryUsage() 

Method Source Code

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

public class Main {
    public static String getMemoryUsage() {
        String result = "free: " + format(Runtime.getRuntime().freeMemory());
        result += ", max: " + format(Runtime.getRuntime().maxMemory());
        return result;
    }//w w  w.  ja v  a 2 s .c o  m

    private static String format(long mem) {
        double dMem = mem;

        String[] units = new String[] { "", "KB", "MB", "GB" };
        for (String unit : units) {
            if (dMem < 1024)
                return String.format("%.1f %s", dMem, unit);

            dMem /= 1024;
        }

        return String.format("%.1f %s", dMem * 1024, units[units.length - 1]);
    }
}

Related

  1. getMemoryInfo()
  2. getMemoryInfo()
  3. getMemoryInfo()
  4. getMemorySize(long size)
  5. getMemoryStatus()
  6. getMemoryUsage()
  7. getMemoryUsage()
  8. getMemoryUsage()
  9. getMemoryUsage()