Java Memory memoryInfo()

Here you can find the source of memoryInfo()

Description

memory Info

License

Apache License

Declaration

public static String memoryInfo() 

Method Source Code

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

public class Main {
    public static String memoryInfo() {
        Runtime runtime = Runtime.getRuntime();
        StringBuilder sb = new StringBuilder();
        long maxMemory = runtime.maxMemory();
        long allocatedMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();

        sb.append("heap: ").append(format(allocatedMemory - freeMemory));
        sb.append(", allocated: ").append(format(allocatedMemory));
        sb.append(", free: ").append(format(freeMemory));
        sb.append(", total free: ").append(format(freeMemory + maxMemory - allocatedMemory));
        sb.append(", max: ").append(format(maxMemory));

        return sb.toString();
    }//from w  w  w .jav  a 2s  .  c  om

    private static String format(long mem) {
        return Long.toString((long) (mem / 1024. / 1024.)) + "MB";
    }
}

Related

  1. measureMemoryBefore()
  2. memory()
  3. memory()
  4. memory()
  5. memoryEqual(byte[] left, int leftOffset, byte[] right, int rightOffset, int length)
  6. memoryIsLow()
  7. memoryOccupied()
  8. memorySize(long bytesSize)
  9. memoryUtilization()