Wednesday, January 21, 2015

Java Examples | Files | Memory Mapped File



Why use Memory Mapped File or MapppedByteBuffer
Memory 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.

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. 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.

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.

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.


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. 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.

Java Examples | Files | Memory Mapped File
Loading large files into jvm may take up a lot of time and may also fail becuase the jvm memory may become full. 

It is now possible to directly map the huge files without loading them into memory. A buffer is created around the file in the file system without loading the file into jvm. The file can be directly read or written using the mapped buffer. This functionality now enables java to now handle large files.

A FileChannel represents a connection to a file which can be used for reading and writing.
http://www.javacodegeeks.com/2013/05/power-of-java-memorymapped-file.html
A memory-mapped file is a segment of virtual memory which has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. This resource is typically a file that is physically present on-disk, but can also be a device, shared memory object, or other resource that the operating system can reference through a file descriptor. Once present, this correlation between the file and the memory space permits applications to treat the mapped portion as if it were primary memory.

MappedByteBuffer mem =fc.map(FileChannel.MapMode.READ_WRITE, 0, bufferSize);
http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ314_029.htm
Memory-mapped files allow you to create and modify files that are too big to bring into memory. With a memory-mapped file, you can pretend that the entire file is in memory and that you can access it by simply treating it as a very large array.
MappedByteBuffer, which is a particular kind of direct buffer. Note that you must specify the starting point and the length of the region that you want to map in the file; this means that you have the option to map smaller regions of a large file.

Although the performance of “old” stream I/O has been improved by implementing it with nio, mapped file access tends to be dramatically faster.

Read full article from Java Examples | Files | Memory Mapped File

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts