Search code examples
javamemory-mapped-files

Does MappedByteBuffer use mmap under linux?


Very simple question, assuming the case under Linux, not windows.

If I try to get a MappedByteBuffer for a huge file with a buffer size very large, will JVM use virtual memory underneath?

Also, will MappedByteBuffer really have page fault? Should I make sure RAM has the free space as the appointed buffer size?


Solution

  • The documentation for MappedByteBuffer clearly states:

    A direct byte buffer whose content is a memory-mapped region of a file.

    On a POSIX system, this means that the JRE will be using mmap to map the file's contents into memory. Unless the file is already present in memory for some reason, this will result in a page fault on the first access for each missing page, and a virtual-memory system can encounter page faults at any time for any allocated memory. Monitor system I/O to see whether you have enough RAM in the system running the application, but the kernel is good at determining when to swap contents in and out if necessary.