Read maximum heap space at runtime

后端 未结 1 1094
我寻月下人不归
我寻月下人不归 2021-02-12 22:51

As we all know, the java -Xmx option is used to set the maximum heap space available to a Java program. But is there a way for a Java program to read the value that

相关标签:
1条回答
  • 2021-02-12 23:27

    There is a Runtime.maxMemory, which the documentation says will return the maximum amount of memory the Java Virtual Machine will attempt to use, but it is not specific whether it is the maximum heap memory set when launching the JVM.

    Just as a test, I wrote the following program:

    class MaxMemory {
        public static void main(String[] args) {
            System.out.println(Runtime.getRuntime().maxMemory());
        }
    }
    

    The results of the execution was the following:

    C:\coobird\>java -Xmx64m MaxMemory
    66650112
    
    C:\coobird\>java -Xmx128m MaxMemory
    133234688
    

    This is using Java SE 6 version 1.6.0_12 on Windows.

    Although the values are close to 64 MB (67,108,864 bytes) and 128 MB (134,217,728 bytes), they aren't exactly so.

    0 讨论(0)
提交回复
热议问题