Click here to Skip to main content
Click here to Skip to main content
Go to top

10 Things to Know about Memory Mapped File in Java

, 17 Nov 2013
Rate this:
Please Sign up or sign in to vote.
Memory Mapped IO is one of the fastest IO options available for Java program. If you are writing latency sensitive application, where IO can skew your numbers, then using MemoryMappedByteBuffer is a good option.

What is Memory Mapped File and IO in Java

Memory mapped files are special, 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 the 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 show 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, then it would result in page fault. It can also be used to share data between two processes.

Memory Mapped IO Support in Operating System

Most of the major operating systems 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.

Important Points of Memory Mapped IO in Java

Here are some of the important facts to know about Memory Mapped File 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 cannot 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 allows two different processes to access File.
  8. 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.
  9. Prefer Direct Byte buffer over Non Direct Buffer for faster performance.
  10. 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.
  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.

That's all folks. Memory mapped IO is an important concept for advanced Java developers, especially for writing high performance applications in Java. If you like this tip, then you may like to visit my blog http://javarevisited.blogspot.com for more such articles.

Thank you!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Javin Paul
Technical Lead
United States United States
Hi, I am Javin Paul, I have just joined this forum. I have been working in Java, FIX Tutorial and TIbco RV messaging technology from past 7 years. I am interested in writing and meeting people, reading and learning about new subjects and I have my own website http://javarevisited.blogspot.com that takes up most of my time and is a labor of love for me. If you have a page I should visit then please let me know
Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 17 Nov 2013
Article Copyright 2013 by Javin Paul
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid