Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8284642

Unexpected behavior of -XX:MaxDirectMemorySize=0

XMLWordPrintable

    • b20
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      openjdk version "17.0.2" 2022-01-18
      OpenJDK Runtime Environment Homebrew (build 17.0.2+0)
      OpenJDK 64-Bit Server VM Homebrew (build 17.0.2+0, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      According to the manual of JDK 17, the behavior of "-XX:MaxDirectMemorySize=size" is described as "By default, the size is set to 0, meaning that the JVM chooses the size for NIO direct-buffer allocations automatically".
      It says when size is set to 0, the JVM will choose the size of direct memory automatically. But when I set "-XX:MaxDirectMemorySize" to 0. I can not allocate even a byte.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just run the following code with "-XX:MaxDirectMemorySize=0".

      java -XX:MaxDirectMemorySize=0 Main.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No error.
      ACTUAL -
      java -XX:MaxDirectMemorySize=0 Main.java
      Exception in thread "main" java.lang.OutOfMemoryError: Cannot reserve 8192 bytes of direct buffer memory (allocated: 0, limit: 0)
              at java.base/java.nio.Bits.reserveMemory(Bits.java:178)
              at java.base/java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:121)
              at java.base/java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:332)
              at java.base/sun.nio.ch.Util.getTemporaryDirectBuffer(Util.java:243)
              at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:293)
              at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:273)
              at java.base/sun.nio.ch.FileChannelImpl.read(FileChannelImpl.java:232)
              at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:65)
              at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:107)
              at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:101)
              at java.base/java.io.BufferedInputStream.read1(BufferedInputStream.java:282)
              at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:343)
              at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:270)
              at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:313)
              at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
              at java.base/java.io.InputStreamReader.read(InputStreamReader.java:177)
              at java.base/java.io.BufferedReader.fill(BufferedReader.java:162)
              at java.base/java.io.BufferedReader.read1(BufferedReader.java:213)
              at java.base/java.io.BufferedReader.read(BufferedReader.java:287)
              at jdk.compiler/com.sun.tools.javac.launcher.Main.readFile(Main.java:254)
              at jdk.compiler/com.sun.tools.javac.launcher.Main.compile(Main.java:371)
              at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:189)
              at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)

      ---------- BEGIN SOURCE ----------
      import java.nio.ByteBuffer;

      public class Main {

          public static void main(String[] args) throws Exception {
              System.out.println(ByteBuffer.allocateDirect(1));
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Do not pass "-XX:MaxDirectMemorySize" to java or set a sufficient size for direct memory.

      FREQUENCY : always


            hseigel Harold Seigel (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: