I have a dictionary saved in a file. I am loading the dictionary into memory from the python interactive shell, and my system monitor says that the python process takes 4 GB. The following commands give the following outputs:
size1 = sys.getsizeof(mydict)/(1024**2)
print size1
96
size2 = 0
for i in mydict.keys():
size2 += sys.getsizeof(i)
print size2/(1024**2)
37
size3 = 0
for i in mydict.keys():
size3 += sys.getsizeof(mydict[i])
print size3/(1024**2)
981
size4 = 0
for i in mydict.keys():
for j in mydict[i]:
size4 += j
print size4/(1024**2)
2302
print str(size1 + size2 + size3 + size4)
3416
Now if i delete the dictionary
del(mydict)
gc.collect()
less than 400 MB are freed from memory. Even if I delete all the elements first from one of the lists inside the dictionary, the freed memory is not more than 450-500 MB. Therefore, I do not get any variables in my shell, but still consumes 3.5 GB. Can someone explain what is happening?
source
share