Here is my test code for python3:
#! /usr/bin/python3
from memory_profiler import profile
@profile(precision=10)
def f():
huge_list = [x for x in range(2000)]
del huge_list
print("finish")
if __name__ == "__main__":
f()
output:
Line # Mem usage Increment Line Contents
================================================
4 17.2109375000 MiB 17.2109375000 MiB @profile(precision=10)
5 def f():
6 17.2109375000 MiB 0.0000000000 MiB huge_list = [x for x in range(2000)]
7 17.2109375000 MiB 0.0000000000 MiB del huge_list
8 17.2226562500 MiB 0.0117187500 MiB print("finish")
It shows that huge_list = [x for x in range(2000)]
doesn't take any memory.
I changed it to huge_list = "aa" * 2000
, it's the same.
But if I change 2000 to 20000, it gets some memory.
why ?
A similar question is here: What does “del” do exactly?