Memory overhead and performance trade‑offs when using matfile to access large .mat variables versus full load on limited RAM
0 reputation · 13 Oct 2022, 08:59 UTC
0 reputation · 13 Oct 2022, 08:59 UTC
33775 reputation · 13 Oct 2022, 17:49 UTC
When a variable in a -v7.3 MAT-file approaches or exceeds physical RAM, load will attempt to materialize the entire array in contiguous workspace memory and can fail with an out-of-memory error. matfile avoids that spike by reading only the indexed subset you request, so peak memory is bounded by the slice size plus HDF5 library and chunk-cache overhead, not by the total variable size. The trade-off is latency: each matfile read incurs disk I/O and HDF5 overhead, so many small slice reads are typically slower than one bulk load when the data does fit in RAM. When the array does not fit, matfile is usually the only viable option.
Confirmed: matfile requires the file to be saved with -v7.3; older formats do not support partial loading. Confirmed: load restores all variables and needs enough RAM for the full dataset. Confirmed: matfile uses lazy, indexed access.
Uncertain or environment-dependent: there is no single published “maximum overhead” number for matfile access to variables larger than 2 GB. Observed overhead depends on slice dimensions, HDF5 chunking, storage layout, OS file cache, and MATLAB version. In practice, requesting a small slice adds only the slice array plus internal buffers; requesting a slice that is a large fraction of the total array makes matfile behave increasingly like load. Iterative slicing generally keeps the resident set stable, but repeated allocation and deallocation of temporaries can contribute to fragmentation on long-running sessions.
-v7.3, matfile will not provide partial access; re-save it in v7.3 format first if possible.memory before and after a representative matfile read, and wrap the operation in tic/toc. Compare that against a full load only if the array actually fits in RAM.clear slice variables inside loops to reduce fragmentation risk.Are you reading many small slices scattered across the array, or a few large contiguous blocks? If the access pattern is scattered and tiny, matfile latency may dominate and a different storage layout (for example, re-chunking the HDF5 dataset) could matter more than the choice between load and matfile.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.