Java Memory Usage getMemoryInfo()

Here you can find the source of getMemoryInfo()

Description

get Memory Info

License

Open Source License

Declaration

public static String getMemoryInfo() 

Method Source Code


//package com.java2s;
import java.text.DecimalFormat;

public class Main {

    public static String getMemoryInfo() {

        DecimalFormat f1 = new DecimalFormat("#,###KB");
        DecimalFormat f2 = new DecimalFormat("##.#");

        Runtime rt = Runtime.getRuntime();
        long free = rt.freeMemory() / 1024;
        long total = rt.totalMemory() / 1024;
        long max = rt.maxMemory() / 1024;
        long used = total - free;
        double ratio = (used * 100 / (double) total);

        StringBuilder sb = new StringBuilder();

        sb.append("Java memory info : ");
        sb.append("used=");
        sb.append(f1.format(used));//from w  ww  .  ja  va2s. c  om
        sb.append(" (");
        sb.append(f2.format(ratio));
        sb.append("%), ");
        sb.append("total=");
        sb.append(f1.format(total));
        sb.append(", ");
        sb.append("max=");
        sb.append(f1.format(max));

        return sb.toString();
    }
}

Related

  1. estimateMemoryUsage(String s)
  2. getMemberName(int number)
  3. getMemInfo()
  4. getMemoryFootprint()
  5. getMemoryInfo()
  6. getMemoryInfo()
  7. getMemorySize(long size)
  8. getMemoryStatus()
  9. getMemoryUsage()