According to [various](http://blog.asquareb.com/blog/2015/06/05/java-direct-bytebuffer-performance-advantages-and-considerations/) [sources](https://stackoverflow.com/a/5671880/5899161) (though not specifically mentioned in JavaDoc), `ByteBuffer.allocateDirect` allocates the memory off the main JVM heap. I can confirm that using Java Mission Control, seeing that the program that calls `ByteBuffer n = ByteBuffer.allocateDirect(Integer.MAX_VALUE)` does not use much of Java Heap memory:

[![enter image description here](https://i.stack.imgur.com/esxju.png)](https://i.stack.imgur.com/esxju.png)

However, this off-heap memory allocation stops working when one limits JVM heap memory. For example, when I run the JVM with `-Xmx1g` option, the `allocateDirect` call causes the following exception: `Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory`. I do not fully understand how can this JVM option pertain to off-heap direct memory allocation, as - according to the [documentation](https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html) - the `-Xmx` option sets the Java heap space size. If I allocate the memory using `getUnsafe().allocateMemory(Integer.MAX_VALUE);` the memory is allocated successfully. My JVM is as follows:

> 
> java version "10" 2018-03-20 Java(TM) SE Runtime Environment 18.3
>  (build 10+46) Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46,
>  mixed mode)
> 
> 
> 

Is this kind of behaviour between `Xmx` and `ByteBuffer.allocateDirect` expected?

EDIT: There seemed to be a (non-reproducible) [bug](https://bugs.java.com/view_bug.do?bug_id=8145536) in JDK 1.7 with the same behaviour as described above. So is this a bug?