Skip to content

RawArray._init_kwargs blocks garbage collection by retaining the input NumPy ndarray #14074

Closed

Description

@orikanner

Description of the problem

BaseRaw.__init__ stores constructor arguments in self._init_kwargs using _get_argvalues().

For RawArray, this includes the complete signal array:

raw._init_kwargs["data"] is raw._data  # True

After operations such as resample() replace raw._data, _init_kwargs["data"] continues retaining the original full-resolution array. Because raw.copy() uses a deep copy, the retained array is copied as well.

For large RawArray objects, this can keep several gigabytes of unused data alive. The cost can multiply when objects are copied or passed to worker processes, potentially causing severe memory pressure, memory failures, and poor parallel scaling.

How I encountered this

I found this while memory profiling a multi stage pipeline for high-sampling-rate signals, where the initial RawArray data can be multiple gigabytes.

After downsampling from 10 kHz to ~200 Hz, the active data became much smaller, but memory profiling showed that the original full resolution array was still retained.

Proposed fix

One possible narrow fix would be to remove the stored data argument locally in RawArray.__init__:

super().__init__(
    info,
    data,
    first_samps=(int(first_samp),),
    dtype=dtype,
    verbose=verbose,
)
self._init_kwargs.pop("data", None)

This leaves the active signal in self._data while avoiding changes to _get_argvalues() or other readers.

I would be glad to submit a PR with a regression test if this approach looks reasonable.

Steps to reproduce

import gc
import weakref

import mne
import numpy as np

mne.set_log_level("ERROR")

data = np.zeros((64, 1_000_000), dtype=np.float64)  # ~512 MB
data_ref = weakref.ref(data)

raw = mne.io.RawArray(
    data,
    mne.create_info(64, 10_000.0, "eeg"),
)

print("aliased:", raw._init_kwargs["data"] is raw._data)

del data
raw.resample(200)
gc.collect()

print("current _data:", round(raw._data.nbytes / 1e6), "MB")
print("retained data:", round(raw._init_kwargs["data"].nbytes / 1e6), "MB")
print("original collected:", data_ref() is None)

raw_copy = raw.copy()

print(
    "copied retained data:",
    round(raw_copy._init_kwargs["data"].nbytes / 1e6),
    "MB",
)
print(
    "same retained array:",
    raw_copy._init_kwargs["data"] is raw._init_kwargs["data"],
)

Link to data

No response

Expected results

After deleting the original data variable and replacing raw._data during resampling, the original full-resolution array should be released from memory.

In this example, RawArray should keep only the current data of about 10 MB, instead of also keeping the unused original array of about 512 MB.

Actual results

aliased: True

# Active downsampled data:
current _data: 10 MB

# Unused original array is still retained:
retained data: 512 MB
original collected: False

# copy() creates another copy of the retained array:
copied retained data: 512 MB
same retained array: False

Additional information

Python 3.13.8 (tags/v3.13.8:a15ae61, Oct 7 2025, 12:34:25) [MSC v.1944 64 bit (AMD64)]
CPU Intel(R) Core(TM) Ultra 7 258V (8 cores)
Memory 31.5 GiB

Core

  • mne 1.12.1 (latest release)
  • numpy 2.5.1 (unknown linalg bindings (threadpoolctl module not found: No module named 'threadpoolctl'))
  • scipy 1.18.0
  • matplotlib 3.11.1 (backend=tkagg)

Numerical (optional)

  • unavailable sklearn, numba, nibabel, nilearn, dipy, openmeeg, cupy, pandas, h5io, h5py

Visualization (optional)

  • unavailable pyvista, pyvistaqt, vtk, qtpy, ipympl, pyqtgraph, mne-qt-browser, ipywidgets, trame_client, trame_server, trame_vtk, trame_vuetify

Ecosystem (optional)

  • unavailable mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline, neo, eeglabio, edfio, curryreader, mffpy, pybv, pymef, antio, defusedxml

Activity

  1. welcome commented on Jul 18, 2026

    @welcome

    Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴

  2. drammock commented on Jul 19, 2026

    @drammock
    Member

    PR welcome! Thanks for the detective work, this sounds like a welcome improvement

  3. orikanner commented on Jul 19, 2026

    @orikanner
    Author

    @drammock I noticed that PR #14075 was opened by an automated coding agent and currently has some failing checks. Would you be able to assign the issue to me? I’d be happy to submit and maintain a PR for the fix.
    Thanks again!

  4. wmvanvliet commented on Jul 20, 2026

    @wmvanvliet
    Contributor

    I like the simple self._init_kwargs.pop("data", None) fix. Go for it! And then please minimally modify one of the existing tests to assert that "data" not in raw._init_kwargs. Don't forget to add yourself to the new contributors list and add a changelog entry (just ask if you need pointers on how to do this).

  5. larsoner commented on Jul 20, 2026

    @larsoner
    Member

    IIRC this is used (only?) by MNE-BIDS, so we (maintainers, unless @orikanner you're up for looking) should also cross-check that this doesn't break anything there. But I think there it's used to potentially reload the data from scratch (or similar), which isn't something that RawArray is really meant to be able to do. So maybe we "just" need to add a guard at the MNE-BIDS end for RawArray at some point. But this is maybe a deep edge case we don't need to worry about!

  6. drammock commented on Jul 20, 2026

    @drammock
    Member

    @drammock I noticed that PR #14075 was opened by an automated coding agent and currently has some failing checks. Would you be able to assign the issue to me? I’d be happy to submit and maintain a PR for the fix. Thanks again!

    we don't have a practice of assigning issues to specific people, so you can feel free to open a PR. #14075 has bee closed.

  7. orikanner commented on Jul 21, 2026

    @orikanner
    Author

    Sounds good, I'll take a look at MNE-BIDS usage and open the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions