Pytest keeps reference to attributes of failed test cases for too long #12198
Labels
topic: tracebacks
related to displaying and handling of tracebacks
type: performance
performance or memory problem/improvement
Hi,
While running some tests with large numpy arrays, I figured out that there is a difference in how pytest cleans up the data for the failed and passed test cases.
For the failed tests, the references to local variables are kept, leading to the accumulation of many large arrays in the memory and eventually out-memory error.
It could probably be best explained with examples
I also logged memory consumption using the following pytest fixture
And here is the result. Nothing is unexpected.

But if one test fails, the second one will fail too as there is no longer enough memory in the python process.
I used
objgraph
to trace the reference to the array. It looks like a frame used by the traceback hold onto it.I think this could be a bug in pytest and may be there is a known work around for this issue.
Pytest 8.1.1
Python 3.11.0
OS: Ubuntu 22.04.3 LTS
Install Packages
Package Version
iniconfig 2.0.0
numpy 1.26.4
packaging 24.0
pip 23.3.1
pluggy 1.4.0
psutil 5.9.8
pytest 8.1.1
setuptools 68.2.2
wheel 0.41.2
pip list
from the virtual environment you are usingThe text was updated successfully, but these errors were encountered: