23

I am slightly confused when I use the getsizeof method in the sys module for dictionaries. Below I have created a simple dictionary of two strings. The two strings' sizes are clearly larger than the one of the dictionary. The dictionary size is probably the dictionary overhead only, i.e., it doesn't take the actual data into account. What is the best way to figure out the memory-usage of the whole dictionary (keys, values, dictionary overhead)?

>>> first = 'abc'*1000
>>> second = 'def'*1000
>>> my_dictionary = {'first': first, 'second': second}
>>> getsizeof(first)
3021
>>> getsizeof(second)
3021
>>> getsizeof(my_dictionary)
140
c00kiemonster
  • 20,805
  • 33
  • 89
  • 129

3 Answers3

13

From the PythonDocs

See recursive sizeof recipe for an example of using getsizeof() recursively to find the size of containers and all their contents.

So it only counts the overhead, but you can use the function in this link to calculate it for containers like dicts.

Jacob
  • 39,723
  • 6
  • 76
  • 81
5

The recursive getsizeof would get the actual size, but if you have multiple layers of dictionaries and only want to get a rough estimate. The json comes handy.

>>> first = 'abc'*1000
>>> second = 'def'*1000
>>> my_dictionary = {'first': first, 'second': second}
>>> getsizeof(first)
3049
>>> getsizeof(second)
3049
>>> getsizeof(my_dictionary)
288
>>> getsizeof(json.dumps(my_dictionary))
6076
>>> size = getsizeof(my_dictionary)
>>> size += sum(map(getsizeof, my_dictionary.values())) + sum(map(getsizeof, my_dictionary.keys()))
>>> size
6495
Chris.Q
  • 1,310
  • 16
  • 20
  • 1
    Definitely points for creativity, but it needs everything to be serializable, it is slower, and as you say it's an approximation... – Mark Apr 30 '17 at 18:33
4

Well, dictionaries don't store the actual string inside them, it works a bit like C/C++ pointers, so you only get a constant overhead in the dictionary for every element.

The total size is

size = getsizeof(d)
size += sum(map(getsizeof, d.itervalues())) + sum(map(getsizeof, d.iterkeys()))
orlp
  • 106,415
  • 33
  • 201
  • 300