Important Facts about Memory Mapped files in Java – thanks to Java Revisited @ Blogspot
- Java supports Memory mapped IO with
- Memory mapped files is used in performance sensitive application, e.g. high frequency electronic trading platforms.
- By using memory mapped IO, you can load portion of large files in memory.
- Memory mapped file can result in page fault if requested page is not in memory.
- Ability to map a region of file in memory depends on addressable size of memory. In a 32 bit machine, you cannot access beyond 4GB or 2^32.
- Memory mapped IO is much faster than Stream IO in Java.
- Memory used to load File is outside of Java heap and reside on shared memory which allows two different processes to access File.
- Reading and writing on memory mapped file is done by operating system, so even if your Java program crashes after putting content into memory, it will make to File until OS is fine.
- Prefer Direct Byte buffer over Non Direct Buffer for faster performance.
- Don’t call
MappedByteBuffer.force()method too often, this method is meant to force operating system to write content of memory into disk, So if you call force() method each time you write into memory mapped file, you will not see true benefit of using mapped byte buffer, instead it will be similar to disk IO.
- In case of power failure or host failure, there is slim chance that content of memory mapped file is not written into disk, which means you could lose critical data.