57

那里有类似的问题,但他们似乎避免回答这个具体问题。如何通过 Java 的 Runtime api获取我的 Java 程序使用的内存?

这里的答案表明我可以做这样的事情:

System.out.println("KB: " + (double) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024);

但这总是返回相同的数字,无论我运行哪个程序。例如,下面我有一个程序,无论我在地图中输入多少数字,内存使用量都保持不变。

package memoryTest;

import java.util.HashMap;
import java.util.Map;

public class MemoryTest {

    static Map<Integer, NewObject> map = new HashMap<Integer, NewObject>();

    public static void main(String[] args){

        System.out.println("KB: " + (double) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024);
        fillMemory(25);

        System.out.println("KB: " + (double) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024);
    }

    static int j=0;
    public static void fillMemory(int i){

        for(int k=0; k< 2000; k++)
            map.put(j++, new NewObject());

    }
    

    public static class NewObject{
        long i = 0L;
        long j = 0L;
        long k = 0L;
    }
    
}

通过cambecc的main方法,输出为:

3085,总计:128516096,免费:127173744,差异:671120

173579,总计:128516096,免费:110033976,差异:671128

335207,总计:128516096,免费:92417792,差异:637544

672788,总计:224198656,免费:159302960,差异:1221520

1171480,总计:224198656,免费:106939136,差异:1221544

1489771,总计:368377856,免费:227374816,差异:1212984

1998743,总计:368377856,免费:182494408,差异:1212984

4

5 回答 5

96
于 2013-06-29T05:02:34.380 回答
4

我有以下方法

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

它以字节为单位返回(使用的)内存。

如果您想重新计算为 MiB,我有:

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


    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) + "%";
    }
于 2018-03-26T09:47:34.193 回答
1
public static void memoryStats() {
    int mb = 1024 * 1024;
    // get Runtime instance
    Runtime instance = Runtime.getRuntime();
    System.out.println("***** Heap utilization statistics [MB] *****\n");
    // available memory
    System.out.println("Total Memory: " + instance.totalMemory() / mb);
    // free memory
    System.out.println("Free Memory: " + instance.freeMemory() / mb);
    // used memory
    System.out.println("Used Memory: "
            + (instance.totalMemory() - instance.freeMemory()) / mb);
    // Maximum available memory
    System.out.println("Max Memory: " + instance.maxMemory() / mb);
}

参考:这里

于 2020-06-13T14:05:07.200 回答
0

以下可以在您的 java 方法中使用以获取与内存相关的统计信息。

// Get the Java runtime
Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
runtime.gc();
// Calculate the used memory
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Used memory is bytes: " + memory);
System.out.println("Used memory is megabytes: "
+ bytesToMegabytes(memory));
于 2019-10-01T06:01:34.007 回答
0

公平的免费内存:

maxMemory() - totalMemory() + freeMemory()

它将与以下内容相同:

maxMemory() - (used memory), where (used memory) = totalMemory() - freeMemory()

因为freeMemory()给你的只是里面的空闲可用内存totalMemory(),但totalMemory()还是可以长大的maxMemory()

于 2021-07-01T08:13:37.497 回答