XX:MaxDirectMemorySize 的默认值是什么?
XX:MaxDirectMemorySize 的默认值是什么?
从 sun.misc.VM
中得知,Runtime.getRuntime.maxMemory()
是通过 -Xmx
参数配置的。例如,如果你没有配置 -XX:MaxDirectMemorySize
参数,但却配置了 -Xmx5g
参数,那么 "默认" 的 MaxDirectMemorySize
也将为5 GB,并且应用程序的堆和直接内存使用总量可能增长到5+5=10GB。
来自http://www.docjar.com/html/api/sun/misc/VM.java.html
我看到:
163 // A user-settable upper limit on the maximum amount of allocatable direct
164 // buffer memory. This value may be changed during VM initialization if
165 // "java" is launched with "-XX:MaxDirectMemorySize=<size>".
166 //
167 // The initial value of this field is arbitrary; during JRE initialization
168 // it will be reset to the value specified on the command line, if any,
169 // otherwise to Runtime.getRuntime.maxDirectMemory().
170 //
171 private static long directMemory = 64 * 1024 * 1024;
看起来它默认为64兆字节。
JDK8:
最初设定的64MB是任意的,...
(来源:https://github.com/frohoff/jdk8u-dev-jdk/blob/master/src/share/classes/sun/misc/VM.java#L186)
// A user-settable upper limit on the maximum amount of allocatable direct
// buffer memory. This value may be changed during VM initialization if
// "java" is launched with "-XX:MaxDirectMemorySize=<size>".
//
// The initial value of this field is arbitrary; during JRE initialization
// it will be reset to the value specified on the command line, if any,
// otherwise to Runtime.getRuntime().maxMemory().
//
private static long directMemory = 64 * 1024 * 1024;
...但是如果没有设置maxDirectMemorySize参数,则会将directMemory设置为maxMemory() ~= Heapsize(从此处设置):
(来源:https://github.com/frohoff/jdk8u-dev-jdk/blob/master/src/share/classes/sun/misc/VM.java#L286)
// Set the maximum amount of direct memory. This value is controlled
// by the vm option -XX:MaxDirectMemorySize=<size>.
// The maximum amount of allocatable direct buffer memory (in bytes)
// from the system property sun.nio.MaxDirectMemorySize set by the VM.
// The system property will be removed.
String s = (String)props.remove("sun.nio.MaxDirectMemorySize");
if (s != null) {
if (s.equals("-1")) {
// -XX:MaxDirectMemorySize not given, take default
directMemory = Runtime.getRuntime().maxMemory();
} else {
long l = Long.parseLong(s);
if (l > -1)
directMemory = l;
}
}
这个测试似乎支持这个说法,"test.java.nio.Buffer.LimitDirectMemory.java":
if (size.equals("DEFAULT"))
return (int)Runtime.getRuntime().maxMemory();
java -XX:+PrintFlagsFinal -version
命令打印所有标志的默认值和当前值。添加| grep Direct
来过滤输出并显示您要查找的标志 :) - jocull