Skip to content

Fix memory leak: implement __deepcopy__ for ExtraCoords and GlobalCoords - #991

Closed
sumitsaraswat362 wants to merge 3 commits into
sunpy:mainfrom
sumitsaraswat362:fix/ndcube-deepcopy
Closed

sumitsaraswat362 wants to merge 3 commits into
sunpy:mainfrom
sumitsaraswat362:fix/ndcube-deepcopy

Conversation

@sumitsaraswat362

Copy link
Copy Markdown

Fixes #989

Description

This PR addresses the issue where ExtraCoords and GlobalCoords retain a plain reference to their parent cube in _ndcube. Because they did not have a custom __deepcopy__, calling deepcopy() on either object would inadvertently deep-copy the entire parent cube (including data, uncertainty, and mask).

This resulted in massive memory allocation spikes during arithmetic operations (e.g. cube * 2) and in NDCube._new_instance, as two full copies of the cube were created and immediately discarded when the coordinates were reassigned.

Fix:
Implemented __deepcopy__ in both ExtraCoords and GlobalCoords to copy the internal tables but leave _ndcube as None. The descriptor handles re-pointing the _ndcube reference upon assignment to the new cube. This effectively removes the memory leak and significantly reduces the peak memory footprint of derived cubes and arithmetic operations.

(Following the smallest/safest change recommended in #989 Option 1)

@Cadair

Cadair commented Oct 22, 2026

Copy link
Copy Markdown
Member

We've run with #996 instead, but thanks.

@Cadair Cadair closed this Oct 22, 2026
@sumitsaraswat362
sumitsaraswat362 deleted the fix/ndcube-deepcopy branch October 23, 2026 07:07
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.

Arithmetic and copy=True deep-copy the whole cube through ExtraCoords._ndcube and GlobalCoords._ndcube

2 participants