lev*_*tov 27

sun.misc.VM,它Runtime.getRuntime.maxMemory()就是配置的内容-Xmx.E. g.如果您配置-XX:MaxDirectMemorySize配置-Xmx5g,"默认" MaxDirectMemorySize也将是5 GB,以及应用程序的总堆+直接内存使用量可能会增长到5 + 5 = 10 GB.


Joh*_*ner 18

来自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;
Run Code Online (Sandbox Code Playgroud)

因此它似乎默认为64兆.

  • 这个答案仍然不正确.正如代码中的注释所述,第171行上的值是任意的,稍后会重置.这是因为即使在命令行中未指定`-XX:MaxDirectMemorySize`,也始终设置属性"sun.nio.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. (7认同)
  • docs.oracle.com链接指向JRockit的文档,而不是OpenJDK. (4认同)
  • 这是不正确的答案,http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/55f6804b4352/src/share/classes/sun/misc/VM.java#l279 (4认同)
  • 这——http://docs.oracle.com/cd/E15289_01/doc.40/e15062/optionxx.htm#BABGCFFB——直接矛盾,声称它是“无限的”? (2认同)

小智 8

对于 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;
Run Code Online (Sandbox Code Playgroud)

...但随后 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;
      }
  }
Run Code Online (Sandbox Code Playgroud)

测试似乎支持这个说法,“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();
Run Code Online (Sandbox Code Playgroud)