Unexpectedly large memory requirements after test teardown #1287
Replies: 14 comments
-
|
Thank you - that indeed doesn't sound good. I can think of a few things that increase memory usage with That this code takes more memory (compared to the test without |
Beta Was this translation helpful? Give feedback.
-
|
I can confirm that the problem is reproducible even on my Windows notebook. The memory usage increases for about 1 GB each test run (which is expected, as this is the size of the files), but only decreases about every 15 tests (e.g. after it has reached 15 GB), when the GC runs. A GC run should not be needed to free the memory, as far as I understand. |
Beta Was this translation helpful? Give feedback.
-
|
Actually the cyclic relationship is quite obvious - a file has a backlink to the filesystem, which is not a weak reference (but should be). Somehow I have never seen this... |
Beta Was this translation helpful? Give feedback.
-
|
Note to self: making all file system references weakrefs does not resolve the problem. |
Beta Was this translation helpful? Give feedback.
-
|
@konnerthg - I have replaced all backlinks by weak references in main branch, as far as I can see. This should fix the problem, though I would like you to test it if possible. I may have introduced some potential crashes due to life time issues. |
Beta Was this translation helpful? Give feedback.
-
|
@mrbean-bremen thank you very much for handling this. on the above minimal example, the memory requirements on my machine indeed go down from 8GiB to 2GiB. that looks very good. However, on our real application, I have not yet managed to see an improvement. I hope there is a mistake on my end. I will get back to you as soon as I figure our more details. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks - I may have missed something, as I used the minimal example for reference testing. |
Beta Was this translation helpful? Give feedback.
-
|
@konnerthg - did you find out something? |
Beta Was this translation helpful? Give feedback.
-
|
i am sorry, i have not been able to reproduce anything in a minimal example after your fix. the memory consumption is about 30% lower. no need to call |
Beta Was this translation helpful? Give feedback.
-
|
Thank you, that's interesting! That basically means that the fake fs itself is still hold by someone. |
Beta Was this translation helpful? Give feedback.
-
|
I added a comment to #1273 of a failure I have found in large scale testing. |
Beta Was this translation helpful? Give feedback.
-
|
Consider that this change seems to be resolving a corner case usage of |
Beta Was this translation helpful? Give feedback.
-
|
@davidlbaird - as discussed in the other issue, I tend to agree. The potential problems are probably not worth the benefit. |
Beta Was this translation helpful? Give feedback.
-
|
I'm going to move this into the discussion area as a limitation. As I wrote - I agree that this is a corner case that is probably not worth to handle. I added a respective section to the troubleshooting guide. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
In the test suite of a django project, memory seems to not always be freed after each test execution. I expect the memory occupied by the fake file system to go down drastically during each teardown. If this is wrong, I'm looking forward to learn the correct behavior.
I use
docker container statsto check my container's memory requirements. I know this isn't a proper benchmark, but the results are easy to see. During the test suite, the memory fills up and the container crashes. I narrowed this down to the behavior in the following example.On my machine, it quickly reaches 3+ GiB of memory. I recommend stopping the execution before it fills, as this produces a long and annoying freeze on my computer. When I manually run garbace collection though (see commented code), the memory usage jumps between 1-2 GiB and goes back to near 0 after each test. Note that you might have to play around with
NUM_TESTSandFILE_SIZE.I also tried running the same example without fake filesystem and the memory usage is negligible.
How To Reproduce
Your environment
Linux-6.12.54-linuxkit-x86_64-with-glibc2.41
Python 3.12.12 (main, Dec 30 2025, 00:43:35) [GCC 14.2.0]
pyfakefs 6.0.0
pytest 9.0.2
requirements.txt:
django==6.0
pytest-django==4.11.1
pyfakefs==6.0.0
Note that django might not be relevant to the problem. it was just easy for me to reproduce it inside a docker container which uses django.
Beta Was this translation helpful? Give feedback.
All reactions