Python: does the garbage collector run before a MemoryError is thrown?

In Python code, which iterates over a sequence of 30 problems related to numerical computations with memory and processor, I noticed that the memory consumption of the Python process increases by ~ 800 MB with the beginning of each of the 30 iterations and finally raises MemoryErrorto the 8th iterations (where system memory is actually exhausted). However, if I import gclet gc.collect()run after each iteration, then the memory consumption remains constant at ~ 2.5 GB, and Python code stops after solve all 30 problems. The code uses only data from two consecutive problems, and there are no reference cycles (otherwise manual garbage collection will also not be able to reduce memory consumption).

Question

This behavior raises the question of whether Python is trying to run the garbage collector before it raises the value MemoryError. In my opinion, that would be perfectly cool, but maybe there are reasons against it?

A similar observation with the above has been done here: https://stackoverflow.com/a/316618/

+4
source share
1 answer

Actually there are reference loops, and this is the only reason why manual calls gc.collect()can even restore memory.

Python ( CPython) , . , , .

, : http://docs.python.org/2/library/gc.html

TL;DR - , Python . (allocations - deallocations) 700 ( 0), , reset.

, ( gc.collect()), 0 ( , ) (.. , - , , , ). , , 1.

10 ( 1), 1, 1, , 2. 10 1 ( 100 - 2), 2. , , 2 - 3.

, gc.set_threshold(threshold0, threshold1, threshold2).

:

  • GC , CPython (refcounting is). GC "" , .
  • , , GC , MemoryError.
  • . .
+4

Source: https://habr.com/ru/post/1532032/


All Articles