http://howtodoinjava.com/2015/01/16/java-nio-2-0-memory-mapped-files-mappedbytebuffer-tutorial/
Once established, a mapping remains in effect until the
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.
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.
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.
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.- The user process sees the file data as memory, so there is no need to issue
read()
orwrite()
system calls. - 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.
- The virtual memory subsystem of the operating system will perform intelligent caching of the pages, automatically managing memory according to system load.
- The data is always page-aligned, and no buffer copying is ever needed.
- Very large files can be mapped without consuming large amounts of memory to copy the data.
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.
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.