How to Force Unrar to Use Memory Mapping

This guide explains how to enable memory-mapped file access when extracting archives with Unrar. Because the official RARLAB Unrar binary does not provide a native command-line switch for memory mapping (mmap), standard extraction relies on conventional buffered file I/O. To bypass this limitation and force memory-mapped behavior, you must leverage operating system mechanisms such as page cache locking, memory-backed filesystems, custom I/O wrappers, or source-level modifications.


Understanding Unrar's File I/O Architecture

The standard Unrar utility uses synchronous stream-based I/O primitives—specifically ReadFile on Windows and standard POSIX read()/write() calls on Unix-like platforms. There is no built-in flag (such as --mmap or -io) to toggle memory mapping directly from the CLI.

To achieve the performance benefits and random-access speeds associated with memory mapping, choose one of the implementation strategies below.


Method 1: Preload and Map Files Using vmtouch (Linux)

If your goal is to eliminate disk read latency and map the archive into system memory prior to extraction, use vmtouch. This utility forces the Linux kernel to map the archive into the page cache.

  1. Install vmtouch:

    sudo apt install vmtouch   # Debian/Ubuntu
    sudo pacman -S vmtouch     # Arch Linux
  2. Map and lock the archive into memory:

    vmtouch -tl archive.rar
    • -t issues an mmap() call and reads each page to bring the file into cache.
    • -l locks the mapped pages in memory using mlock(), preventing the kernel from paging them out.
  3. Extract using Unrar:

    unrar x archive.rar
  4. Release locked memory when done:

    vmtouch -e archive.rar

Method 2: Use Memory-Backed Filesystems (tmpfs / RAM Disk)

The most robust way to force Unrar to execute operations strictly within memory is to stage the archive inside a memory-mapped RAM filesystem.

On Linux (tmpfs):

A tmpfs mount is directly backed by the Linux virtual memory manager and page cache.

  1. Create a mount point and mount a temporary memory filesystem:
    sudo mkdir -p /mnt/ramdisk
    sudo mount -t tmpfs -o size=8G tmpfs /mnt/ramdisk
  2. Move the target archive into /mnt/ramdisk and extract:
    cp archive.rar /mnt/ramdisk/
    cd /mnt/ramdisk
    unrar x archive.rar

On Windows:

Use a virtual RAM disk driver (such as ImDisk) to allocate a drive letter directly from RAM, copy the RAR archive to that drive, and execute unrar.exe against the mounted volume.


Method 3: Intercept I/O via an LD_PRELOAD Wrapper

On Unix systems, you can intercept the standard read and open system calls invoked by unrar and redirect them through an mmap interface using dynamic linker hooking.

  1. Write a minimal C wrapper that intercepts calls to open() and read(). When Unrar opens the target file, call mmap() on the file descriptor:
    // mmap_wrapper.c
    #define _GNU_SOURCE
    #include <stdio.h>
    #include <stdlib.h>
    #include <dlfcn.h>
    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    // Intercept standard open/read routines and route file buffers via mmap
  2. Compile into a shared library:
    gcc -Wall -fPIC -shared -o libmmap_wrap.so mmap_wrapper.c -ldl
  3. Run Unrar with the library preloaded:
    LD_PRELOAD=./libmmap_wrap.so unrar x archive.rar

Method 4: Patch the Source Code

Because RARLAB publishes the portable Unrar source code, you can permanently implement memory mapping by altering how archive files are opened.

  1. Download the Unrar source code package from RARLAB.
  2. Open file.cpp (or os.cpp).
  3. Locate the File::Open and File::Read routines.
  4. Replace the underlying read() or ReadFile() calls:
    • Linux: Use mmap(NULL, FileSize, PROT_READ, MAP_SHARED, Handle, 0). Advance a memory pointer inside File::Read instead of issuing synchronous read operations.
    • Windows: Use CreateFileMapping() and MapViewOfFile() to map the entire archive into the process's address space.
  5. Recompile the binary using make -f makefile and use the resulting executable for extraction.