I have a question about space in the memory used by list
and dict
objects.
My Python 3.7 shell shows:
>>> import sys
>>> list_ = []
>>> sys.getsizeof(list_)
64
>>> dict_ = {}
>>> sys.getsizeof(dict_)
240
>>> list_ += dict_,
>>> list_
[{}]
>>> sys.getsizeof(list_)
96
I don't understand what is going on.
- The
list
object takes64
bytes of memory (what's shown in shell)+ 8
bytes for each element. The
dict
object takes240
bytes of memory.So, after adding the dictionary as an element, the
list
should take64 + 8 + 240 = 312
bytes.
Why did the memory taken by the list increase by only 32
bytes? What happened to the dict
's 240
bytes memory? And why did the amount of memory used increase by just 32
bytes?