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

ByteBuffer.allocateDirect depends on -Xmx JVM parameter

    XMLWordPrintable

Details

    • x86_64
    • generic

    Description

      A DESCRIPTION OF THE PROBLEM :
      When starting JVM without -Xmx parameter ByteBuffer.allocateDirect() successfully allocates off-heap memory. However, when one limits JVM heap on startup with -Xmx parameter (like -Xmx1g) the amount of memory that ByteBuffer.allocateDirect() can allocate is limited by the size of JVM heap. Off-heap memory allocation should not depend on JVM on-heap configuration options.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the following source code and pass -Xmx1g option to JVM:
      import java.nio.ByteBuffer;

      class Scratch_1 {
          public static void main(String[] args) {
              ByteBuffer bb = ByteBuffer.allocateDirect(Integer.MAX_VALUE);
          }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program runs and allocates Integer.MAX_VALUE bytes of off-heap memory.
      ACTUAL -
      The exception is thrown:

      Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
      at java.base/java.nio.Bits.reserveMemory(Bits.java:187)
      at java.base/java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
      at java.base/java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:310)
      at Scratch_1.main(Scratch_1.java:5)

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

      class Scratch_1 {
          public static void main(String[] args) {
              ByteBuffer bb = ByteBuffer.allocateDirect(Integer.MAX_VALUE);
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Activity

          People

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: