Get real RAM usage of current JVM using Java

In HTOP, you can see the RES (resident size) value, which shows how much of your RAM your JVM process really takes.

Now I want to get this value only using pure Java. If you tell me that this is simply not possible, that is also good.

Let me explain all the attempts that I made with an application that shows the value of RES 887M in htop:

  • Combining a devotee with HeapMemory and non-Heapmemory through MemoryMXBean gives me 726M
  • Adding all the written memory for all ManagementFactory.getMemoryPoolMXBeans () gives me 737M
  • runtime.totalMemory () gives 515M

Any ideas what else I could try?

+4
source share
2 answers

Assuming you are using Linux, just read the file /proc/self/statusand find the line VmRSS:.

Or analyze /proc/self/smapsto get comprehensive memory information for the current process.

+3
source

I wrote:

public class RuntimeUtil {
private static final long MEGABYTE_FACTOR = 1024L * 1024L;
private static final DecimalFormat ROUNDED_DOUBLE_DECIMALFORMAT;
private static final String MIB = "MiB";

static {
    DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
    otherSymbols.setDecimalSeparator('.');
    otherSymbols.setGroupingSeparator(',');
    ROUNDED_DOUBLE_DECIMALFORMAT = new DecimalFormat("####0.00", otherSymbols);
    ROUNDED_DOUBLE_DECIMALFORMAT.setGroupingUsed(false);
}

private RuntimeUtil() {
    //No Init
}

public static long getMaxMemory() {
    return Runtime.getRuntime().maxMemory();
}

public static long getUsedMemory() {
    return getMaxMemory() - getFreeMemory();
}

public static long getTotalMemory() {
    return Runtime.getRuntime().totalMemory();
}

public static long getFreeMemory() {
    return Runtime.getRuntime().freeMemory();
}

public static String getTotalMemoryInMiB() {
    double totalMiB = bytesToMiB(getTotalMemory());
    return String.format("%s %s", ROUNDED_DOUBLE_DECIMALFORMAT.format(totalMiB), MIB);
}

public static String getFreeMemoryInMiB() {
    double freeMiB = bytesToMiB(getFreeMemory());
    return String.format("%s %s", ROUNDED_DOUBLE_DECIMALFORMAT.format(freeMiB), MIB);
}

public static String getUsedMemoryInMiB() {
    double usedMiB = bytesToMiB(getUsedMemory());
    return String.format("%s %s", ROUNDED_DOUBLE_DECIMALFORMAT.format(usedMiB), MIB);
}

public static String getMaxMemoryInMiB() {
    double maxMiB = bytesToMiB(getMaxMemory());
    return String.format("%s %s", ROUNDED_DOUBLE_DECIMALFORMAT.format(maxMiB), MIB);
}

public static double getPercentageUsed() {
    return ((double) getUsedMemory() / getMaxMemory()) * 100;
}

public static String getPercentageUsedFormatted() {
    double usedPercentage = getPercentageUsed();
    return ROUNDED_DOUBLE_DECIMALFORMAT.format(usedPercentage) + "%";
}

private static double bytesToMiB(long bytes) {
    return ((double) bytes / MEGABYTE_FACTOR);
}

public static String getHostAdress() {
    try {
        java.net.InetAddress addr = java.net.InetAddress.getLocalHost();
        return addr.getHostAddress();
    } catch (UnknownHostException e) {
        // looks like a strange machine
        System.out.println(e.getMessage());
    }
    return StringUtil.EMPTY;
}

public static String getHostName() {
    try {
        java.net.InetAddress addr = java.net.InetAddress.getLocalHost();
        return addr.getHostName();
    } catch (UnknownHostException e) {
        // looks like a strange machine
        System.out.println(e.getMessage());
    }
    return StringUtil.EMPTY;
}

public static String getSystemInformation() {
    return String.format("SystemInfo=Current heap:%s; Used:%s; Free:%s; Maximum Heap:%s; Percentage Used:%s",
            getTotalMemoryInMiB(),
            getUsedMemoryInMiB(),
            getFreeMemoryInMiB(),
            getMaxMemoryInMiB(),
            getPercentageUsedFormatted());
}
0
source

All Articles