Java Memory Used getUsedMemoryStr()

Here you can find the source of getUsedMemoryStr()

Description

get Used Memory Str

License

Open Source License

Declaration

public static String getUsedMemoryStr() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String getUsedMemoryStr() {
        long totalMem = Runtime.getRuntime().totalMemory();
        long freeMem = Runtime.getRuntime().freeMemory();
        return bytesToString(totalMem - freeMem);
    }//from  w  ww.j av a  2  s.  co m

    public static String bytesToString(long b) {
        double gb = (double) b / (1024 * 1024 * 1024);
        if (gb >= 1)
            return gb >= 10 ? (int) gb + "G" : round(gb, 1) + "G";
        double mb = (double) b / (1024 * 1024);
        if (mb >= 1)
            return mb >= 10 ? (int) mb + "M" : round(mb, 1) + "M";
        double kb = (double) b / (1024);
        if (kb >= 1)
            return kb >= 10 ? (int) kb + "K" : round(kb, 1) + "K";
        return b + "";
    }

    public static double round(double x, int numPlaces) {
        double scale = Math.pow(10, numPlaces);
        return Math.round(x * scale) / scale;
    }
}

Related

  1. getUsedMemory()
  2. getUsedMemoryInMb()
  3. getUsedMemoryInMegabytes()
  4. getUsedMemoryMBs()
  5. getUsedMemoryMegaBytes()
  6. jvmInUseMemory(String size, Boolean txtByte)
  7. makeUsedPhysicalMemoryData(long systemPhysicalMaxValue, long systemPhysicalFreeValue)
  8. measureMemoryUse()
  9. memoryUsed()