Skip to content

FIX: Prevent memory leak in RawArray by removing data from _init_kwargs - #14360

Open
adi-coderr wants to merge 1 commit into
mne-tools:mainfrom
adi-coderr:fix-rawarray-memleak
Open

adi-coderr wants to merge 1 commit into
mne-tools:mainfrom
adi-coderr:fix-rawarray-memleak

Conversation

@adi-coderr

@adi-coderr adi-coderr commented Sep 30, 2026 •

Copy link
Copy Markdown

This fixes: #14074

Description of the problem

BaseRaw.__init__ captures constructor arguments in self._init_kwargs via _get_argvalues(). For RawArray, this includes the complete signal array (self._init_kwargs["data"]).

When operations such as resample() or crop() replace raw._data, self._init_kwargs["data"] continues to hold a strong reference to the original array, preventing it from being garbage collected. In addition, raw.copy() deep-copies _init_kwargs, duplicating the unused dead array and multiplying memory usage.

Solution

  1. Remove "data" from self._init_kwargs in RawArray.__init__ immediately after super().__init__(...).
  2. Update _test_raw_reader(RawArray, ...) in test_array.py to pass test_kwargs=False (since in-memory RawArray objects cannot be re-instantiated without data like disk-based readers).
  3. Add a weakref regression check in test_array_copy() ensuring raw._data replacement allows the original array to be collected and that "data" is absent from _init_kwargs.

Assistance Disclosure

In accordance with the MNE AI-assistance policy in CONTRIBUTING.md, an AI coding assistant was used to help trace the frame inspection in _get_argvalues() and draft the regression test.

@welcome

welcome Bot commented Sep 30, 2026

Copy link
Copy Markdown

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

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant