9

After some indepth reading, all documentation leads to state two things about dictionaries:

  • They instantiate with enough capacity for '8' items
  • They implicitly resize at 2/3 full (4x under 50,000 items and 2x above)

If that is the case, why does this dictionary only consume 368 bytes of RAM, when an empty dictionary takes 240 bytes, shouldn't this of resized 4x, e.g: 960 bytes?

    >>> getsizeof(dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7))
    368
    >>> getsizeof(dict(a=1,b=2,c=3))
    240

Am I being misinformed or misunderstanding something core here? Did something change with regards to this information on python 3.7?

symon
  • 670
  • 1
  • 7
  • 20
  • 1
    Dictionaries were changed slightly in 3.7 to make them ordered (insertion order that is). As such the old rules of thumb for memory have changed. Dictionaries no longer require as many empty buckets to put information. – Error - Syntactical Remorse Apr 30 '19 at 18:16
  • 3
    I would assume the object has overhead in itself, not all of its memory usage is elements. So the amount needed by elements will grow, but the base size stays the same. So it’s not a -> 4a but a+b -> a+4b – Sami Kuhmonen Apr 30 '19 at 18:18
  • interesting, could you provide any reading on how things work after this change? I am doing indepth study on dictionaries, would love to read something relevant to 'now'. the resizing algorithm is the same, just the memory consumption is lower? Also how can we see how much 'capacity' the dictionary has (not to be confused with actual items(len()) – symon Apr 30 '19 at 18:22
  • See https://stackoverflow.com/q/39980323 and https://youtu.be/p33CVV29OG8 – Jeff Mercado Apr 30 '19 at 18:28

1 Answers1

6
import sys
dictt=dict()
array=[]
for i in range(0,1000):
    dictt[i]=i
    array.append(sys.getsizeof(dictt))
print(array)

numberarray=[]
for i in range(1,1001):
    numberarray.append(i)

import matplotlib.pyplot as plt
plt.plot(numberarray,array)
plt.ylabel('memory')
plt.xlabel("items")
plt.show()

enter image description here

now visualize the question helps a lot. you got a nice debating topic. enter image description here

you should refer: http://www.jessicayung.com/how-python-implements-dictionaries/

Jainil Patel
  • 1,284
  • 7
  • 16