使用Java获得当前JVM的实际RAM使用率

时间:2014-10-31 14:24:37

标签: java memory jvm jvm-hotspot

在HTOP中,您可以看到RES值(驻留大小),它显示了您的JVM进程实际占用了多少RAM。

现在我想通过仅使用纯Java获得该值。如果你告诉我这是不可能的,那也没关系。

让我解释一下我用htop显示RES值为887M的应用程序所做的所有尝试:

  1. 通过MemoryMXBean组合提交的HeapMemory和非Heapmemory给了我726M
  2. 添加所有ManagementFactory.getMemoryPoolMXBeans()的所有提交内存给了我737M
  3. runtime.totalMemory()给出515M
  4. 任何想法我还能尝试什么?

2 个答案:

答案 0 :(得分:3)

假设您正在运行Linux,请阅读/proc/self/status文件并查找VmRSS:行。

或解析/proc/self/smaps以获取当前流程的全面内存信息。

答案 1 :(得分:0)

我写过:

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());
}