Friday 13 January 2012

Memory Mapped File in Java - Read Write Code Example

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.nio package. Java IO has been considerably fast after introduction of NIO and memory mapped file offers fastest IO operation possible in Java and quite  useful in high frequency trading space, where electronic trading system needs to be super fast and one way latency to exchange has to be on sub-micro second level. IO has always been concern for performance sensitive applications and memory mapped file allows you to directly read from memory and write into memory. There are some other advantage also which we will see in next section.

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.
Another key 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 memory mapped file, how to read and write from memory mapped file and important points related to Memory Mapped Files.


What are Memory Mapped File and IO in Java

Memory mapped files are special files in Java which allows Java program to access contents  directly from memory, this is achieved by mapping whole file or portion of file into memory and operating system takes care of loading page requested and writing 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. Java programming language supports memory mapped file with java.nio package and has MappedByteBuffer to read and 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 electronic trading system. Memory Mapped Files are way faster than standard file access via normal IO. Another big advantage of memory mapped IO is that it allows you to load potentially larger file which is not otherwise accessible. Experiments shows that memory mapped IO performs better with large files. Though it has disadvantage in terms of increasing number of page faults. since operating system only loads a portion of file into memory if a page requested is not present in memory than it would result in page fault.

Memory Mapped IO support in Operating System
Most of major operating system like Windows platform, UNIX, Solaris and other UNIX like operating system supports memory mapped IO and with 64 bit architecture you can map almost any file into memory and access it directly using Java programming language.

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

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

  public class CanExecuteTest {

    private static int count = 1010241024; //10 MB

    public static void main(String[] args) throws Exception {
        RandomAccessFile memoryMappedFile = new RandomAccessFile("largeFile.txt", "rw");
      
        //Mapping a file into memory
        MappedByteBuffer out = memoryMappedFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, count);
     
        //Writing into Memory Mapped File
        for (int i = 0; i < count; i++) {
            out.put((byte) 'A');
        }
        System.out.println("Writing to Memory Mapped File is completed");
     
        //reading from memory file in Java
        for (int i = 0; i < 10 ; i++) {
            System.out.print((char) out.get(i));
        }
        System.out.println("Reading from Memory Mapped File is completed");
    }
}


Important points of Memory Mapped IO in Java
To summarize the post here is quick summary of memory mapped files and IO in Java:

1) Java supports Memory mapped IO with java.nio package.
2) Memory mapped files is used in performance sensitive application e.g. high frequency electronic trading platforms.
3) By using memory mapped IO you can load 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 machine you 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.
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 File until OS is fine.

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.

No comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More