bhylu.com

Advertisements

NumPy Memory Mapping

A memory map exposes file-backed storage through an array interface. It can help work with a large local file, but does not make every operation memory-free.

How it works

This local-only example uses an owned temporary file and Python mmap as the explicit resource owner. NumPy reads its buffer while the mapping is open; all array views must be released before the mapping closes. Operations that allocate a result still need memory, and real I/O behaviour depends on the operating system and workload. The browser playground is not offered as a demonstration of local disk mapping.

Run the code

This memory-mapping example is local-only. It uses an owned temporary file and an explicit mapping lifetime; the browser editor is not a test of local disk mapping.

For local work, use a separate virtual environment and run python memory-maps.py. Download the complete source. The reviewed local environment uses Python 3.12.14 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.

Example

memory-maps.py

import numpy as np

import mmap
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory(prefix="numpy-map-") as folder:
    file = Path(folder) / "readings.bin"
    file.write_bytes(np.array([12, 18, 24], dtype="<i8").tobytes())
    with file.open("r+b") as stream:
        with mmap.mmap(stream.fileno(), 0) as mapping:
            values = np.frombuffer(mapping, dtype="<i8")
            print(int(values.sum()))
            del values

Expected result

54

The explicit little-endian dtype describes how bytes become integers. Releasing the array before leaving the mapping context avoids retaining a view into a closed resource. The private file disappears with its temporary directory.

Use NumPy’s official reference for precise contracts, supported versions and numerical limitations.