XX:MaxDirectMemorySize 的默认值是多少?
3 回答
从sun.misc.VM
, 它是Runtime.getRuntime.maxMemory()
, 这就是配置的-Xmx
. 例如。如果您不配置并-XX:MaxDirectMemorySize
配置,“默认”也将是 5 Gb,并且应用程序的总堆+直接内存使用量可能会增长到 5 + 5 = 10 Gb。-Xmx5g
MaxDirectMemorySize
来自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;
...但是directMemory 设置为 maxMemory() ~= Heapsize (如果 maxDirectMemorySize-Parameter 未设置):
(来自: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();