Sunday, September 6, 2015

Java Memory Mapped File or MapppedByteBuffer



http://howtodoinjava.com/2015/01/16/java-nio-2-0-memory-mapped-files-mappedbytebuffer-tutorial/
Once established, a mapping remains in effect until the MappedByteBuffer object is garbage collected. Also, mapped buffers are not tied to the channel that created them. Closing the associated FileChannel does not destroy the mapping; only disposal of the buffer object itself breaks the mapping.

  1. The user process sees the file data as memory, so there is no need to issue read() or write() system calls.
  2. As the user process touches the mapped memory space, page faults will be generated automatically to bring in the file data from disk. If the user modifies the mapped memory space, the affected page is automatically marked as dirty and will be subsequently flushed to disk to update the file.
  3. The virtual memory subsystem of the operating system will perform intelligent caching of the pages, automatically managing memory according to system load.
  4. The data is always page-aligned, and no buffer copying is ever needed.
  5. Very large files can be mapped without consuming large amounts of memory to copy the data.
http://javarevisited.blogspot.com/2012/01/memorymapped-file-and-io-in-java.html
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.niopackage and has MappedByteBuffer to read and write from memory.

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.

Another big advantage 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 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.

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.

http://www.javacodegeeks.com/2013/05/power-of-java-memorymapped-file.html
http://tutorials.techmytalk.com/2014/11/05/java-nio-memory-mapped-files/
In Java’s earlier version, It uses FileSystem conventional API to access system file. Behind the scene JVM makes read () and write () system call to transfer data from OS kernel to JVM. JVM utilize it memory space to load and process file that causes trouble on large data file processes. File page convert to JVM system before processing the file data because OS handle file in page but JVM uses byte stream that is not compatible with page. 

In JDK version 1.4 and above Java provides MappedByteBuffer which help to establish a virtual memory mapping from JVM space to filesystem pages. This removes the overhead of transferring and coping the file’s content from OS kernel space to JVM space. OS uses Virtual Memory to cache the file in outside of Kernel space that could be sharable with other non-kernel process. Java maps the File pages to MappedByteBuffer directly and process these file without loading into JVM.

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