Consider the following minimal example:
# used memory: Python2=7421 MB, Python3=7440 MB
a = list(range(10**8))
# used memory: Python2=10553 MB, Python3=11317 MB
a = 1
# used memory: Python2=9785 MB, Python3=7454 MB
# ---> why does Python2 need >2GB of RAM here?
# after python process terminates: Python2=7433 MB, Python3=7458 MB
A large object is created which should be garbage collected after the second line. The memory usage has been monitored using free -m
(this is not an exact measurement of course).
Python 3 needs more memory (3.7GB instead of 3.05GB) to store the large object, but it does what I expected: memory usage drops after the object is not needed any longer. Python2 seems to delete only 768 MB and keep 2.3GB of memory allocated. Why?
This is repeatable: if the list is created a second time, it will use again 3.05 GB, not more and it will drop again to 2.3GB RAM usage. gc.collect() returns 0 and does not change the amount of used memory.
Please don't tell me to use Python 3 - I know... :)
Some links to documentation which did not answer my question:
free -m
. I did not measure the memory used by the process. I measured the occupied memory by any process.