text: Learn about Java memory-mapped files and learn to read and write content from a memory mapped file with the help of RandomAccessFile and MemoryMappedBuffer.
  1. Java Memory-mapped IO
  If you know how java IO works at lower level, then you will be aware of buffer handling, memory paging and other such concepts..
  Read More : Working With Buffers.
  A mapped byte buffer and the file mapping that it represents remain valid until the buffer itself is garbage-collected. 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.
  Example 1: Writing to a memory mapped file
  import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class MemoryMappedFileExample { static int length = 0x8FFFFFF; public static void main(String[] args) throws Exception { try(RandomAccessFile file = new RandomAccessFile("howtodoinjava.dat", "rw")) { MappedByteBuffer out = file.getChannel() .map(FileChannel.MapMode.READ_WRITE, 0, length); for (int i = 0; i < length; i++) { out.put((byte) 'x'); } System.out.println("Finished writing"); } } }
  The file created with the above.
  3. File Mapping Modes
  Like conventional file handles, file mappings can be writable or read-only.
  - The first two mapping modes, MapMode.READ_ONLY and MapMode.READ_WRITE, are fairly obvious. They indicate whether you want the mapping to be read-only or to allow modification of the mapped file.
  - The third mode, MapMode.PRIVATE, indicates that you want a copy-on-write mapping. This means that any modifications you make via
  put()will result in a private copy of the data that only the
  MappedByteBufferinstance can see.
  No changes will be made to the underlying file, and any changes made will be lost when the buffer is garbage collected. Even though a copy-on-write mapping prevents any changes to the underlying file, you must have opened the file for read/write to set up a
  MapMode.PRIVATEmapping. This is necessary for the returned
  MappedByteBufferobject to allow put()s.
  You’ll notice that there is no unmap() method. 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.
  Be careful about how files are manipulated by other threads or external processes when they are memory-mapped. is ever needed.
  - Very large files can be mapped without consuming large amounts of memory to copy the data.
  5. How to read a Memory-Mapped File
  To read a file using memory mapped IO, use below code template:
  import java.io.File; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class MemoryMappedFileReadExample { private static String bigExcelFile = "bigFile.xls"; public static void main(String[] args) throws Exception { try (RandomAccessFile file = new RandomAccessFile(new File(bigExcelFile), "r")) { //Get file channel in read-only mode FileChannel fileChannel = file.getChannel(); //Get direct byte buffer access using channel.map() operation MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); // the buffer now reads the file as if it were loaded in memory. System.out.println(buffer.isLoaded()); //prints false System.out.println(buffer.capacity()); //Get the size based on content size of file //You can read the file from this buffer the way you like. for (int i = 0; i < buffer.limit(); i++) { System.out.print((char) buffer.get()); //Print the content of file } } } }
  6. How to write into a Memory-Mapped File
  To write data into a file using memory mapped IO, use below code template:
  import java.io.File; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class MemoryMappedFileWriteExample { private static String bigTextFile = "test.txt"; public static void main(String[] args) throws Exception { // Create file object File file = new File(bigTextFile); //Delete the file; we will create a new file file.delete(); try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw")) { // Get file channel in read-write mode FileChannel fileChannel = randomAccessFile.getChannel(); // Get direct byte buffer access using channel.map() operation MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 4096 * 8 * 8); //Write the content using put methods buffer.put("howtodoinjava.com".getBytes()); } } }
  Drop me your comments and thoughts in the comments section.
  Happy Learning !!
  13 thoughts on “Java MappedByteBuffer”
  You really should update your code to show how to close the RandomAccessFile and FileChannel (e.g. in try-with-resources), and set the MappedByteBuffer reference to null (to encourage GC), once you have finished with the file.
  Hi,
  Is there any way to access memory files like buffers , streams or … with an address ?
  in an android project ,
  for example I want to access to these memory objects like access to any file in phone storage with an its address
  0x8FFFFFF is not 128 MB
  0x8000000 is 128 MB
  i am fetching data of around 15GB from a Database and creating a file from that. Will memory mapped file help me ?
  Must mention that releasing the memory does not work on some unices as expected. Then you need explicitely call sun package to release memory. Off course the warning for usin sun internal api eill show up.
  to use mapped memory file i need to know just the file name or all the path ??!!!!
  have you heard about relative paths?
  Hi,
  I like to check how to read a file more that 2GB. Can I define the offset and split the file and make more than one read? I was not successful doing it. Can you please throw your ideas on it.
  Thanks
  Sudhagar C
  Have you tried code written in “Reading a Memory-Mapped File”? How bog file it was able to handle in your system?
  I’d like to add that I’ve created a library, MappedBus), which supports having multiple processes write records in order to the same memory mapped file as well as having multiple processes reading records from it.
  Thanks for sharing it.
  Awesome lib. Thanks Mikael!
  Not sure what you learned or how chronicle inspired your. Your impl is much cleaner!
  Thanks for the clear tutorial, it helped me a lot.
  I was searching for a way to read quickly bytes in large file and I was using a ReadableByteChannel. I compared both methods and it seems that the MappedByteBuffer is faster.