I'm trying to understand how DirectByteBuffer works on Linux and wrote the following very simple program to run under strace:
public static void main(String[] args){ while(true){ ByteBuffer buffer = ByteBuffer.allocateDirect(8192); } } I expected actually some mmap or sys_brk syscalls to allocate memory from the operating system directly, but actually it just sets read and write protection of the pages requested. I mean something like:
mprotect(0x7fa9681ef000, 8192, PROT_READ|PROT_WRITE) = 0 This seems the reason that allocating direct buffer is slower than allocating heap buffer since it requires syscall for each allocation.
Please correct me if I'm wrong, but heap buffer allocation (if happens inside TLAB) is equivalent to returning a pointer to pre-allocated heap memory.
QUESTION: Why can't we do the same for direct memory? Return a pointer to pre-allocated memory?
mmapin some cases: "Crashes on Linux 64. Using mprotect instead of mmap/munmap when possible stopped the crashes and prevented us from leaking guard pages"