In a Python code that iterates over a sequence of 30 problems involving memory- and CPU-intense numerical computations, I observe that the memory consumption of the Python process grows by ~800MB with the beginning of each of the 30 iterations and finally raises an MemoryError
in the 8th iteration (where the system's memory is in fact exhausted). However, if I import gc
and let gc.collect()
run after each iteration, then the memory consumption remains constant at ~2.5GB and the Python code terminates nicely after solving all 30 problems. The code only uses the data of 2 consecutive problems and there are no reference cycles (otherwise the manual garbage collection would also not be able to keep the memory consumption down).
The question
This behavior raises the question if Python tries to run the garbage collector before it raises an MemoryError
. In my opinion, this would be a perfectly sane thing to do but perhaps there are reasons against this?
A similar observation to the above was made here: https://stackoverflow.com/a/4319539/1219479