javajvmhuge-pagesoff-heap

Java off heap memory and huge pages


When allocating off-heap memory in Java (through direct buffers, or JNI native code for example), will the allocated memory be backed by huge pages if the JVM is using -XX:+UseLargePages ?


Solution

  • No, HotSpot JVM uses a plain libc malloc call to allocate memory for a direct ByteBuffer.

    However, if you replace the standard system allocator with, for example, jemalloc - you'll be able to configure malloc to use huge pages when available.

    Another option to use huge pages for direct ByteBuffers is to create a file on a hugetlbfs filesystem and then map it in Java as a MappedByteBuffer.