Default for XX:MaxDirectMemorySize

后端 未结 3 1042
太阳男子
太阳男子 2021-01-01 09:31

What is the default value for XX:MaxDirectMemorySize?

3条回答
  •  借酒劲吻你
    2021-01-01 10:26

    For JDK8:

    The 64MB are set arbitrarily initially, ...

    (From: 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=".
        //
        // 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;
    

    ... but then the directMemory is set to maxMemory() ~= Heapsize here (if the maxDirectMemorySize-Parameter is not set):

    (from: 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=.
      // 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;
          }
      }
    

    The test seems to support this claim, "test.java.nio.Buffer.LimitDirectMemory.java":

    (from 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();
    

提交回复
热议问题