在运行时读取最大堆空间 [英] Read maximum heap space at runtime
问题描述
众所周知, java -Xmx
选项用于设置Java程序可用的最大堆空间。但Java程序是否有办法读取已设置的值?类似 System.getMaximumHeapSpace()
(不存在)。
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 has been set? Something like System.getMaximumHeapSpace()
(which does not exist).
推荐答案
Runtime.maxMemory
,文档说明将返回Java虚拟机将尝试使用的最大内存量,但它是否是特定的最大堆内存集是否具体在启动JVM时。
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());
}
}
执行结果如下:
C:\coobird\>java -Xmx64m MaxMemory
66650112
C:\coobird\>java -Xmx128m MaxMemory
133234688
这是使用Java SE Windows上的6版本1.6.0_12。
This is using Java SE 6 version 1.6.0_12 on Windows.
虽然值接近64 MB(67,108,864字节)和128 MB(134,217,728字节),但它们并非如此。
Although the values are close to 64 MB (67,108,864 bytes) and 128 MB (134,217,728 bytes), they aren't exactly so.
这篇关于在运行时读取最大堆空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!