What is Memory Mapped File in Java



Memory mapping is a technique in Java that gives direct access to files through memory (RAM). A memory-mapped file connects disk content straight to RAM, which lets Java applications handle files as memory segments. This makes files to be treated as a large array and is much faster than regular file operations like read() or write(). This requires fewer system calls and makes file operations faster even for very large files.

The java.nio package helps us to implement memory-mapped file operations. This package contains the MappedByteBuffer class, which enables efficient file reading and writing from memory.

Use cases of memory-mapped files

  • High-performance applications ? Enables fast file retrieval with minimal delays.

  • Handling Large Files ? Efficiently processes files larger than available RAM size.

  • Inter-Process Communication (IPC) ? Allows fast data sharing between programs.

  • Database tasks ? Improves data storage and retrieval speed, especially for very large data sets.

  • Reducing garbage collection overhead ? Since the data is stored outside the Java heap, it reduces the garbage collection pauses and improves the performance of the application.

Difference between Memory-mapped and Standard file I/O

Some important differences between memory-mapped I/O and standard file I/O are listed in the following table.

Memory-Mapped I/O Standard File I/O
Faster due to direct memory mapping Slower due to the extra steps for copying data from disk to the program's memory
Reduced system call overhead System calls for each read/write operation
Data is stored outside the Java heap, therefore there is no garbage collection overhead. Data is stored inside the Java heap, therefore it requires garbage collection, which makes it slower.

Types of memory-mapped files in Java

Java offers three memory mapping options depending upon how you interact with the files ?

  • Read-only mode ? The file is mapped only for reading. Attempting to modify the file will throw an exception. It is suitable for log files or configuration files.

  • Read-write mode ? Changes made in the file in the memory are automatically saved back to the file on the disk. It is useful for updating files and databases and for real-time analytics.

  • Private mode ? Modifications made are not saved to the file. The changes are only seen in the current process. It is suitable for temporary file manipulations without affecting the original file.

Implementation of memory mapped files in Java

Key components used

RandomAccessFile: Opens a file for reading as well as writing.

FileChannel: Executes file operations more efficiently.

MappedByteBuffer: represents the file mapped in memory.

map(): maps file segments into memory.

Put(): writes data into memory

get(): reads data from memory

Force(): Ensures changes are saved to the disk.

Steps for implementation

Below are the steps required to create and use a memory-mapped file efficiently in Java.

Step 1: Import the necessary Java I/O and NIO classes for file handling and memory mapping.

Step 2: Define the file name and size for the memory-mapped file.

Step 3: Use RandomAccessFile to open the file and FileChannel to map it into memory.

Step 4: Store characters 'a' and 'b' at positions 0 and 1 in the memory using the put() method.

Step 5: Retrieve the stored characters using the get() method.

Step 6: Use force() to save these changes into the disk.

Step 7: Use a try-catch block to manage I/O errors properly.

Implementation code

The following Java program demonstrates how to implement memory-mapped files using the java.nio package.

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class MemoryMappedFileExample {
   public static void main(String[] args) {
      String filename = "example.dat";
      long fileSize = 10 * 1024 * 1024; // 10MB

      try (RandomAccessFile file = new RandomAccessFile(filename, "rw");
         FileChannel fileChannel = file.getChannel()) {

         MappedByteBuffer mappedRegion = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileSize);

         mappedRegion.put(0, (byte) 97); // 'a'
         mappedRegion.put(1, (byte) 98); // 'b'

         System.out.println((char) mappedRegion.get(0)); // Output: a
         System.out.println((char) mappedRegion.get(1)); // Output: b

         mappedRegion.force(); // Save changes to disk
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Note

The first access to a page is slow because the OS must load data from disk. This is known as a page fault. Later accesses happen really fast as the data remains in memory. Large files exceeding the RAM capacity trigger multiple page faults, which will decrease the performance.

Major Advantages of Memory-Mapped Files

  • Allows high speed file access, especially for very large files.
  • No garbage collection overhead as data is stored outside the Java heap.
  • Allows inter-process communication efficiently

Major Disadvantages of Memory-Mapped Files

  • It is not possible to extend the memory-mapped file size directly, without remapping.
  • Data may not be saved unless we manually save it using a buffer.force() method.
  • Multiple page faults may decrease the performance.
Updated on: 2025-02-28T14:42:52+05:30

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements