You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory usage increases rapidly as the number of tests grows #10605
Using Python 3.13 and pytest 9.2, the reproduction showed approximately:
60 MiB RSS increase for a normal run with 5,000 parametrized tests
32 MiB RSS increase with --collect-only
7 MiB RSS increase for 1,000 tests with the terminal plugin disabled
This does not by itself prove a single leak, but it demonstrates that memory usage grows substantially with the number of collected parametrized items. The results also suggest that collection/parametrization metadata accounts for a significant part of the baseline, while reporting and terminal handling add additional overhead during normal execution.
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
Thanks for the repro. I re-measured on current main (Python 3.13), splitting RSS into collection and run phases, with and without tracemalloc:
N
collect
run
run with -p no:terminal
1k
2.6 MiB
2.5 MiB
0.4 MiB
5k
14.0 MiB
13.6 MiB
2.1 MiB
10k
27.2 MiB
27.3 MiB
5.0 MiB
Observations:
tracemalloc roughly doubles RSS. With it enabled I get the numbers from the repro: 59 MiB at 5k, and 31 MiB with --collect-only. Without it, the totals are 27.6 MiB and 14.3 MiB.
Collection costs about 2.8 KB per item, which is the item, callspec, keywords and nodeid. It grows linearly, which is expected.
The run phase costs about 2.8 KB per test, and nearly all of it comes from the terminal reporter. It keeps every TestReport in stats: 30,000 reports at 10k tests (setup, call and teardown). Without it, the run phase costs about 0.5 KB per test. Not keeping the passed setup/teardown reports in stats[''] could be a real saving.
None of this explains the original report. That one showed about 10 MB per 40 tests, which is about 250 KB per test and roughly 50 times more than this. That suggests something suite-specific is being retained. A measurement on the fpdf2 suite itself that counts objects still alive after each test would be the useful next step.
I reproduced this on Python 3.13 with pytest 9.2. The growth is reproducible and roughly linear with the number of parametrized cases.
In collection-only runs, TestReport and CollectReport counts were zero, while one Function item and one CallSpec were retained per parameter case. The largest allocations came from pytest’s node, parametrization, mark, and fixture metadata.
This suggests high parametrized collection memory retention rather than an unbounded leak. The test body and retained test object do not appear to be the source. Execution can add report-related memory on top of the collection cost.
Hi! I'd like to investigate this issue as an external contributor.
I reviewed the recent memory profiling results, particularly the memory retained by the terminal reporter in its stats collection.
Would you be open to a focused contribution that reduces unnecessary retention of passed setup/teardown reports, provided the existing reporting behavior and test compatibility are preserved?
I'd first prepare a benchmark and regression tests to validate the memory improvement.
Please let me know if this direction would be useful.
Activity
RonnyPfannschmidt commentedon Dec 22, 2022
I wonder how much this changes with the -s flag or disabling the logging plugin
Lucas-C commentedon Dec 22, 2022
On my computer, measures were similar when using
-sor-p no:logging, no big difference.nicoddemus commentedon Dec 23, 2022
Definitely something is fishy, we routinely have thousands of tests in our suites at work, and we don't see a memory growth like that.
TobiMcNamobi commentedon Mar 28, 2025
Is #619 Very large test suites take far too much RAM related?
RonnyPfannschmidt commentedon Mar 28, 2025
Nope
More than 100k tests is a entirely different league than this
pourushsiddharth commentedon Oct 30, 2026
Here is a minimal reproduction for the memory growth observed with large parametrized test suites in this issue:
Reproduction files
Example
Using Python 3.13 and pytest 9.2, the reproduction showed approximately:
--collect-onlyThis does not by itself prove a single leak, but it demonstrates that memory usage grows substantially with the number of collected parametrized items. The results also suggest that collection/parametrization metadata accounts for a significant part of the baseline, while reporting and terminal handling add additional overhead during normal execution.
RonnyPfannschmidt commentedon Oct 30, 2026
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
Thanks for the repro. I re-measured on current main (Python 3.13), splitting RSS into collection and run phases, with and without
tracemalloc:-p no:terminalObservations:
tracemallocroughly doubles RSS. With it enabled I get the numbers from the repro: 59 MiB at 5k, and 31 MiB with--collect-only. Without it, the totals are 27.6 MiB and 14.3 MiB.TestReportinstats: 30,000 reports at 10k tests (setup, call and teardown). Without it, the run phase costs about 0.5 KB per test. Not keeping the passed setup/teardown reports instats['']could be a real saving.Generated by Claude Code
pourushsiddharth commentedon Oct 30, 2026
I reproduced this on Python 3.13 with pytest 9.2. The growth is reproducible and roughly linear with the number of parametrized cases.
In collection-only runs,
TestReportandCollectReportcounts were zero, while oneFunctionitem and oneCallSpecwere retained per parameter case. The largest allocations came from pytest’s node, parametrization, mark, and fixture metadata.This suggests high parametrized collection memory retention rather than an unbounded leak. The test body and retained test object do not appear to be the source. Execution can add report-related memory on top of the collection cost.
AkaashParmar commentedon Nov 11, 2026
Hi! I'd like to investigate this issue as an external contributor.
I reviewed the recent memory profiling results, particularly the memory retained by the terminal reporter in its stats collection.
Would you be open to a focused contribution that reduces unnecessary retention of passed setup/teardown reports, provided the existing reporting behavior and test compatibility are preserved?
I'd first prepare a benchmark and regression tests to validate the memory improvement.
Please let me know if this direction would be useful.