XX的默认值:MaxDirectMemorySize

43

XX:MaxDirectMemorySize 的默认值是什么?


1
我记得默认值是取-Xmx的值。这应该通过一个简单的测试来验证。 - irreputable
4
直接内存用于与堆无关的特定事情。根据我的研究,似乎64是默认值,将值设置为-1会将其设置为-Xmx。 - John Gardner
2
作为参考,您可以使用 java -XX:+PrintFlagsFinal -version 命令打印所有标志的默认值和当前值。添加 | grep Direct 来过滤输出并显示您要查找的标志 :) - jocull
3个回答

50

sun.misc.VM 中得知,Runtime.getRuntime.maxMemory() 是通过 -Xmx 参数配置的。例如,如果你没有配置 -XX:MaxDirectMemorySize 参数,但却配置了 -Xmx5g 参数,那么 "默认" 的 MaxDirectMemorySize 也将为5 GB,并且应用程序的堆和直接内存使用总量可能增长到5+5=10GB。


21

来自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兆字节。


2
这个链接 -- http://docs.oracle.com/cd/E15289_01/doc.40/e15062/optionxx.htm#BABGCFFB -- 直接与之相矛盾,声称它是“无限的”。 - StaxMan
4
这个 docs.oracle.com 的链接指向的是 JRockit 的文档,而不是 OpenJDK 的。 - user61051
1
这个注释不正确,根据我对代码的理解。但不仅因为没有这样的方法。说“此字段的初始值是任意的”这部分似乎是错误的。请参见http://www.docjar.com/html/api/sun/misc/VM.java.html上第253行的链接。在openJDK代码中,如果没有MaxDirectMemorySize的值,则使用默认值64M。如果存在MaxDirectMemorySize并设置为-1,则使用Runtime.getRuntime().maxMemory()。 - Cheeso
4
这是一个错误的答案,链接为http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/55f6804b4352/src/share/classes/sun/misc/VM.java#l279。 - jmj
9
这个答案仍然是不正确的。正如代码中的注释所述,第171行的值是任意的并且稍后会被重置。这是因为“sun.nio.MaxDirectMemorySize”属性总是被设置的,即使在命令行上没有指定“-XX:MaxDirectMemorySize”。在6b27中,它位于hotspot/src/share/vm/prims/jvm.cpp的第344行(从选项复制值到属性)和hotspot/src/share/vm/runtime/globals.hpp的第3530行(默认值为-1)。在7u40中,它位于jvm.cpp的349-357行,在8u40中,它位于jvm.cpp的359-367行。 - jade
显示剩余4条评论

13

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":

(来源于https://github.com/frohoff/jdk8u-dev-jdk/blob/da0da73ab82ed714dc5be94acd2f0d00fbdfe2e9/test/java/nio/Buffer/LimitDirectMemory.java#L74

 if (size.equals("DEFAULT"))
            return (int)Runtime.getRuntime().maxMemory();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接