Pages

Memory Mapped Files in Java is rather new java concept for many programmers and developers,though it’s been there from JDK 1.4 along with java.niopackage. Java IO has been considerably fastafter the introduction of NIO and memory mapped file offers fastest IO operation possible in Java, that's the main reason of Why high-performance Java application should use Memory Mapped files for persisting data. It's already quite popular in high-frequency trading space, where the electronic trading system needs to be super fast and one-way latency to exchange has to be on the sub-micro second level. IO has always been a concern for performance-sensitive applications andmemory mapped file allows you to directly read from memory and write into memory by using direct and non-direct Byte buffers.
Key advantage of Memory Mapped File is that operating system takes care of reading and writing and even if your program crashed just after writing into memory. OS will take care of writing content to File. One more notable advantage is shared memory, memory mapped files can be accessed by more than one process and can be act as shared memory with extremely low latency. See Peter's comment also on comment section.Earlier we have seen how to read xml file in Java and how to read text file in java and in this Java IO tutorial we gonna look on what is memorymapped file, how to read and write from memory mapped file and important points related to Memory MappedFiles.

What is Memory Mapped File and IO in Java

Memory mapped files are special files in Javawhich allows Java program to access contents directly from memory, this is achieved by mappingwhole file or portion of file into memory and operating system takes care of loading page requested andwriting into file while application only deals with memory which results in very fast IO operations. Memory used to load Memory mapped file is outside of Java heap Space. Javaprogramming language supports memory mapped file with java.nio package and has MappedByteBuffer to readand write from memory.

Advantage and Disadvantage of Memory Mapped file

Possibly main advantage of Memory Mapped IO is performance, which is important to build high frequency electronictrading system. Memory Mapped Files are way faster than standard file access via normal IO. Another bigadvantage of memory mapped IO is that it allows you to load potentially larger file which is not otherwiseaccessible. Experiments shows that memory mapped IO performs better with large files. Though it hasdisadvantage in terms of increasing number of page faults. Since operating system only loads a portion of fileinto memory if a page requested is not present in memory than it would result in page fault Most of major operating system like Windows platform, UNIX, Solaris and other UNIX like operating systemsupports memory mapped IO and with 64 bit architecture you can map almost any file into memory and accessit directly using Java programming language. Another advantages is that the file can be shared, giving you shared memory between processes and can be more than 10x lower latency than using a Socket over loopback.

MappedByteBuffer Read Write Example in Java

Below example will show you how to read and write from memory mapped file in Java. We have used RandomAccesFileto open a File and than mapped it to memory using FileChannel's map() method, map method takes three parametermode, start and length of region to be mapped. It returns MapppedByteBuffer which is a ByteBuffer for dealingwith memory mapped file.

3) By using memory mapped IO you canload portion of large files in memory.

4) Memory mapped file can result in page fault if requested page is not in memory.

5) Ability to map a region of file in memory depends on addressable size of memory. In a 32 bit machineyou can not access beyond 4GB or 2^32.

6) Memory mapped IO is much faster than Stream IO in Java.

7) Memory used to load File is outside of Java heap and reside on shared memory which allow two different process to access File. By the way this depends upon, whether you are using direct or non-direct byte buffer.

8) Reading and writing on memory mapped file is done by operating system, so even if your Java Program crash after putting content into memory it will make to disk, until OS is fine.

9) Prefer Direct Byte buffer over Non Direct Buffer for higher performance.10) Don't call MappedByteBuffer.force() method to 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.11) 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.

12) MappedByteBuffer and file mapping remains valid until buffer is garbage collected. sun.misc.Cleaner is probably the only option available to clear memory mapped file.

That’s all on memory mapped file and memory mapped IO in Java. Its pretty useful concept and I encourage you to learn more about it. If you are working on high frequency trading space than memory mapped file is quite common there.

14 comments
:

Nice article. I would add that changes don't need to be flushed to disk. If you change a memory value and immediately die (e.g. triggering a SIGSEGV) the change is still saved to disk (Provided your OS doesn't die)

Another advantages is that the file can be shared, giving you shared memory between processes and can be more than 10x lower latency than using a Socket over loopback.

For maximum performance you can use Unsafe to access the data directly. ;)

Java memory mapped I/O is quite nice!But I would like to point out that the count number you stated (count = 1010241024) is not 10 MB it's actually 963 MB.Just don't be suprised when the file grows bigger and bigger ;)

Hi Javin, but isn't it true that you cannot unmap a memory mapped file, which can only be done if the garbage collector collects your buffer? In that case isn't memory mapped file a bit useless?

I have read on Peters blog where he used the sun.misc.Cleaner file to clean up the memory mapped file, is this the correct approach? I say this because there is no update on the SUN website regarding this bug

@Anonymous, you are right, MappedByteBuffer and file mapping remains valid until buffer is garbage collected. sun.misc.Cleaner is probably the only option available to clear memory mapped file. If I come across any other method to unmap or clean contents of MappedByteBuffer I will let you know.

How big memory mapped file can be in Java? I read that it can only be 2GB because ByteBuffer, which is often used as MappedByteBuffer uses integer as index, which means only 2GB? What to do to map a file which is larger than 2GB in Java?

Few more things to remember while using Memory Mapped File for high performance application :

1) Prefer Direct Byte buffer over Non Direct Buffer2) Don't call MappedByteBuffer.force() method to 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.3) 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.

Rahul,To use memory mapped files for IPC feel free to check out my library, MappedBus, (http://github.com/caplogic/mappedbus). It supports having multiple processes (JVMs) adding records to the same memory mapped file as well as multiple processes reading from the file.

I’d just like to add that I’ve created a library called MappedBus (http://github.com/caplogic/mappedbus) which enable several Java processes (JVMs) to use a memory mapped file for IPC. For this it makes use of Unsafe, CAS and volatile read/writes. The full explanation of how it’s done is at the github page if you’re interested.