0

Playing with the python interpreter, I found the __sizeof__ method. The Docstrings say that this method returns the size, in bytes, of a given object.

Then, I asked myself: "What could be the size of an int? 16, 32?", but, surprisingly, it's 28... Similarly, a float takes 4 more octets in memory. Plus, the size of an empty string, in memory, is 49 bytes. Finally, I was surprised to see that empty list, tuple, dict, an set take 40, 24, 264 and 200 bytes in memory, while if we add only one int to each of these types, the size goes 8 bytes up for list and tuple, and doesn't change for dict and set... Finally, if we create simple classes, even if we add to them new attributs and methods, their size stayes 32 bytes, but simple functions take 112 bytes...

Why does theses types take this strange amount of memory ? Why adding a new int to differents containers doesn't take the same place in memory, while adding a new attribut to a class doesn't modify it's size ? Why does functions take that much place in memory compared to classical objects ?

Spirine
  • 1,837
  • 1
  • 16
  • 28
  • There's likely some overhead because the value also has to store what type it is, its reference count, etc. – Colonel Thirty Two May 02 '15 at 21:25
  • Yes. Don't forget, there are no primitive types in Python; everything is an object. – Daniel Roseman May 02 '15 at 21:26
  • As well as [Why does sys.getsizeof() not return \[size\] in file.read(\[size\]) in Python](https://stackoverflow.com/q/26680650), [Why do ints require three times as much memory in Python?](https://stackoverflow.com/q/23016610) and [Python generator objects: \_\_sizeof\_\_()](https://stackoverflow.com/q/12477835) – Martijn Pieters May 02 '15 at 21:26
  • Also integers can store arbitrarily high numbers, such as 9999!, so there's not much sense in asking what size they are. – Teyras May 02 '15 at 21:27
  • @DanielRoseman But an empty object takes 32 bytes in memory... – Spirine May 02 '15 at 21:29
  • @Spirine: what 'empty object'? It depends on the exact type of the object what amount the minimum memory footprint is. The C structures used are *not* a simple pointer here; there's a mix of types, based of either `PyObject_HEAD` or `PyObject_VAR_HEAD`. – Martijn Pieters May 02 '15 at 21:37
  • @Spirine: at the very least that means two pointers for the doubly-linked heap list, a reference count, and a type pointer, with `PyObject_VAR_HEAD` adding an object size value (e.g. length of a list or string or other variable size object), in addition to the actual data itself. – Martijn Pieters May 02 '15 at 21:40
  • @MartijnPieters I'm talking about containers, so we call a container empty if the len function returns 0. But does all this explain the factor of almost 5, between the size of an integer in memory, and its _python size_? I'm not here to troll, but just to understand. – Spirine May 02 '15 at 21:59
  • @Spirine: see the posts I already linked you to. An empty Python container still requires bookkeeping; the `PyObject_VAR_HEAD` alone contains 3 pointers plus two integers to count things, plus the array for storing things in plus the array length (which is overallocated). I also linked you to a post that explains, in detail, what memory requirements a Python integer has. These are not primitive C types, these are full-on objects. – Martijn Pieters May 02 '15 at 22:03

0 Answers0