Skip to content

TST: measure retained reducer allocations instead of peak RSS - #602

Merged
neutrinoceros merged 8 commits into
pydata:masterfrom
charan-rathore:tst/memory-test-tracemalloc
Oct 1, 2026
Merged

neutrinoceros merged 8 commits into
pydata:masterfrom
charan-rathore:tst/memory-test-tracemalloc

Conversation

@charan-rathore

@charan-rathore charan-rathore commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #597.

The reducer memory test compares process-wide peak RSS before and after its loop. Unrelated growth can fail that check, while a retained allocation can be missed if it stays below the previous peak.

Measure retained Python and NumPy allocations with tracemalloc after warming up the reducers and collecting garbage. Keep the same reducer calls and axes. Use the noise allowance already established by the error-path leak test and always stop tracing. Remove the Windows exclusion because resource is no longer used.

One test checks that the probe detects retained NumPy arrays.

Validation: 210 repository tests passed on Python 3.12.12 and NumPy 2.5.3 on macOS. The final 10-test memory suite, Ruff lint and formatting, and all configured pre-commit hooks passed.

This does not detect arbitrary untracked C allocations. Windows and free-threaded builds were not tested locally.

@neutrinoceros

Copy link
Copy Markdown
Collaborator

Hi, thank you for working on this.

The repository has no configured typecheck.

This could be a personal note but it feels off here. Could you please disclose any LLM use in this PR ? Thank you.

@charan-rathore

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Yes, this change was drafted with LLM assistance (Instinct assisted), and it was reviewed and tested before submitting. I removed the typecheck sentence from the description.

Comment thread bottleneck/tests/test_memory_probe.py Resolved
Comment thread bottleneck/tests/test_memory_probe.py Resolved
Comment thread bottleneck/tests/memory_test.py Resolved
bn.nanmax(arr, axis=axis)
bn.nanvar(arr, axis=axis)

hammer(50)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

the purpose (warmup) is clear in the context of reviewing the PR, but I think a comment might help future readers too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, added a short comment above the warmup explaining it.

@neutrinoceros neutrinoceros left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

looks good to me now. Though I'll wait 'til https://docs.python.org/3/library/tracemalloc.html is back online in order to run one last sanity check before I merge. Thanks again !

Comment thread bottleneck/tests/memory_test.py Outdated

monkeypatch.setattr(bn, "nansum", leaking_nansum)
with pytest.raises(AssertionError):
test_memory_leak()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The idea I can get behind, but this should really use pytest.mark.xfail(..., strict=True). It would make even more sense to parameterize the original test on the mocked function (one case being effectively "no-mock"), using pytest.param(..., marks=pytest.mark.xfail(..., strict=True) as the second case, replacing this one.

@neutrinoceros neutrinoceros left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I actually would like another change in before merging. Though I could handle it myself if you'd like.

@charan-rathore

Copy link
Copy Markdown
Contributor Author

Thanks, done in 0bfa920: test_memory_leak is now parametrized on the mocked function, one no-mock case and one retained-arrays case marked xfail(strict=True, raises=AssertionError). The separate probe test is gone. I ran it against a released bottleneck wheel only (no-mock passes, retained-arrays xfails), not against this branch's build, so CI is the real check.

Comment thread bottleneck/tests/memory_test.py Outdated
Comment on lines +40 to +41
if leaky:
monkeypatch.setattr(bn, "nansum", _leaking_nansum_factory())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

what I had in mind was to use bn.nansum and _leaking_nansum_function directly in the parametrization and to mock unconditionally (the first case would obviously correspond to no mocks)

@charan-rathore

Copy link
Copy Markdown
Contributor Author

Got it, thanks. Reworked in 78d4cfc: test_memory_leak is parametrized directly on bn.nansum (no-mock) and a module-level _leaking_nansum (strict xfail), and bn.nansum is patched unconditionally in the test. Ran it against a released wheel again: one pass, one xfail. CI will cover the branch build.

Comment thread bottleneck/tests/memory_test.py Outdated
Comment on lines +35 to +36
import gc
import tracemalloc

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

resource couldn't be imported portably at the top level but these could and should

@charan-rathore

Copy link
Copy Markdown
Contributor Author

Done in 2932f2a: gc and tracemalloc are now imported at module level and the function-level imports are removed from both tests.

@neutrinoceros

Copy link
Copy Markdown
Collaborator

pre-commit.ci autofix

@neutrinoceros
neutrinoceros merged commit 6a5977a into pydata:master Oct 1, 2026
42 checks passed
@charan-rathore
charan-rathore deleted the tst/memory-test-tracemalloc branch October 1, 2026 15:45
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.

BUG: test_memory_leak is flaky

2 participants