Default for XX:MaxDirectMemorySize
Asked Answered
B

3

43

What is the default value for XX:MaxDirectMemorySize?

Binal answered 22/9, 2010 at 21:29 Comment(3)
I remember the default is taking the value of -Xmx. This should be verified by a simple test.Christelchristen
direct memory is used for other specific things not related to the heap. from what i found in my exploration, it looks like 64 is the default, using -1 as the value sets it to -Xmx.Taipan
For reference, you can always print out the default and current values of all of the flags using java -XX:+PrintFlagsFinal -version. Add in | grep Direct to filter the output to show the one you're looking for :)Revetment
T
21

From http://www.docjar.com/html/api/sun/misc/VM.java.html

i see:

 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;

so it appears to default to 64 megs.

Taipan answered 22/9, 2010 at 21:56 Comment(9)
This -- docs.oracle.com/cd/E15289_01/doc.40/e15062/… -- directly contradicts it, claiming it'd be "unlimited"?Miscible
The docs.oracle.com link points to the docs for JRockit, not OpenJDK.Schematize
The comment suggests that the value, if not specified on the command line, is taken from maxDirectMemory(). This link here (mail.openjdk.java.net/pipermail/core-libs-dev/2012-January/…) correct the typo here (there's no such method) to maxMemory(), which in turn equals to the -Xmx you set on the command line.Subalpine
The comment is wrong, according to my reading of the code. But not only because there is no such method. The part that says, "The initial value of this field is arbitrary" seems wrong. See line 253 in the link at docjar.com/html/api/sun/misc/VM.java.html . In the openJDK code, in the absence of a value for MaxDirectMemorySize, it uses the default of 64M. If MaxDirectMemorySize is present and is set to -1, then it uses Runtime.getRuntime().maxMemory().Felicitation
@cheeso, yes, that's consistent with what I said in my comment on the original question in 2010 :DTaipan
It is incorrect answer, hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/55f6804b4352/src/share/…Eustasius
that might be the case for jdk7 on openjdk, in May of 2014. but this question was specifically about the default size on the sun 1.6 jvm, back in 2010.Taipan
This answer is still incorrect. As the comment in the code states, the value on line 171 is arbitrary and reset later. This is because the property "sun.nio.MaxDirectMemorySize" is always set, even if -XX:MaxDirectMemorySize is not specified on the command line. In 6b27, it's on hotspot/src/share/vm/prims/jvm.cpp:344 (copies value from option to property) and hotspot/src/share/vm/runtime/globals.hpp:3530 (default of -1). In 7u40, it's jvm.cpp lines 349-357, and in 8u40 it's jvm.cpp 359-367.Orinasal
@leventov: in future, please add a new answer, rather than making such radical edits to someone else's answer.Emulate
S
52

From sun.misc.VM, it's Runtime.getRuntime.maxMemory(), that's what is configured with -Xmx. E. g. if you don't configure -XX:MaxDirectMemorySize and do configure -Xmx5g, the "default" MaxDirectMemorySize will also be 5 Gb, and the total heap+direct memory usage of the app may grow up to 5 + 5 = 10 Gb.

Sherikasherill answered 7/9, 2017 at 19:5 Comment(0)
T
21

From http://www.docjar.com/html/api/sun/misc/VM.java.html

i see:

 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;

so it appears to default to 64 megs.

Taipan answered 22/9, 2010 at 21:56 Comment(9)
This -- docs.oracle.com/cd/E15289_01/doc.40/e15062/… -- directly contradicts it, claiming it'd be "unlimited"?Miscible
The docs.oracle.com link points to the docs for JRockit, not OpenJDK.Schematize
The comment suggests that the value, if not specified on the command line, is taken from maxDirectMemory(). This link here (mail.openjdk.java.net/pipermail/core-libs-dev/2012-January/…) correct the typo here (there's no such method) to maxMemory(), which in turn equals to the -Xmx you set on the command line.Subalpine
The comment is wrong, according to my reading of the code. But not only because there is no such method. The part that says, "The initial value of this field is arbitrary" seems wrong. See line 253 in the link at docjar.com/html/api/sun/misc/VM.java.html . In the openJDK code, in the absence of a value for MaxDirectMemorySize, it uses the default of 64M. If MaxDirectMemorySize is present and is set to -1, then it uses Runtime.getRuntime().maxMemory().Felicitation
@cheeso, yes, that's consistent with what I said in my comment on the original question in 2010 :DTaipan
It is incorrect answer, hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/55f6804b4352/src/share/…Eustasius
that might be the case for jdk7 on openjdk, in May of 2014. but this question was specifically about the default size on the sun 1.6 jvm, back in 2010.Taipan
This answer is still incorrect. As the comment in the code states, the value on line 171 is arbitrary and reset later. This is because the property "sun.nio.MaxDirectMemorySize" is always set, even if -XX:MaxDirectMemorySize is not specified on the command line. In 6b27, it's on hotspot/src/share/vm/prims/jvm.cpp:344 (copies value from option to property) and hotspot/src/share/vm/runtime/globals.hpp:3530 (default of -1). In 7u40, it's jvm.cpp lines 349-357, and in 8u40 it's jvm.cpp 359-367.Orinasal
@leventov: in future, please add a new answer, rather than making such radical edits to someone else's answer.Emulate
R
14

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=<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;

... 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=<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;
      }
  }

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();
Racing answered 30/1, 2019 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.