Skip to content

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

Merged
larsoner merged 2 commits into
mne-tools:mainfrom
adi-coderr:fix-rawarray-memleak
Oct 1, 2026
Merged

larsoner merged 2 commits into
mne-tools:mainfrom
adi-coderr:fix-rawarray-memleak

Conversation

@adi-coderr

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

Copy link
Copy Markdown
Contributor

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. 🚴

@larsoner larsoner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work, thanks @adi-coderr

@larsoner
larsoner enabled auto-merge (squash) October 1, 2026 14:00
@larsoner
larsoner disabled auto-merge October 1, 2026 15:10
@larsoner
larsoner merged commit cf3c2ab into mne-tools:main Oct 1, 2026
23 of 25 checks passed
@welcome

welcome Bot commented Oct 1, 2026

Copy link
Copy Markdown

🎉 Congrats on merging your first pull request! 🥳 Looking forward to seeing more from you in the future! 💪

larsoner added a commit to larsoner/mne-python that referenced this pull request Oct 1, 2026
* upstream/main: (26 commits)
  Splitting _brain.py into multiple modules (phase 1) (mne-tools#14359)
  Fix small corner-case bug with moments (mne-tools#14364)
  Coregistration GUI modernisation (phase 5) (mne-tools#14353)
  FIX: Prevent memory leak in RawArray by removing data from _init_kwargs (mne-tools#14360)
  MAINT: Update code credit (mne-tools#14365)
  type-hint the rest of mne/_fiff (mne-tools#14352)
  MAINT: Update dependency specifiers (mne-tools#14362)
  MAINT: Update dependency specifiers (mne-tools#14357)
  Switch to mne-tools SPEC-0 action (mne-tools#14356)
  MAINT: Move Pixi file updates to autofix workflow (mne-tools#14345)
  BUG: Fix SNIRF landmark length unit scaling (mne-tools#14354)
  DRY dipole code and comment on implementation (mne-tools#14350)
  Speed up CSD and PSD computations (mne-tools#14355)
  [dependabot]: Bump the actions group with 4 updates (mne-tools#14349)
  MAINT: Update pre-commit hook versions (mne-tools#14347)
  Switch to `mne-tools/rename-towncrier` action (mne-tools#14344)
  Fix for latest pyvista-js (mne-tools#14343)
  Avoid copies and support larger files in EEGLAB export (mne-tools#14338)
  Remove line breaks in sponsors check issue text (mne-tools#14337)
  Fix EEGLAB events after dropped epochs (mne-tools#14067)
  ...

# Conflicts:
#	mne/viz/_brain/_brain.py
#	mne/viz/backends/tests/test_renderer.py
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

2 participants