0

Python 3.8.10; Linux Ubuntu

If the integer 5 is referenced in the same memory address for both the integer i and the 1st position in ls list variable, why does the stand alone integer suggest 32 bits, while the same integer in the list (with the same memory address) show as 64 bit?

The confusion comes from the fact that an empty list has an overhead of 56 bytes on my machine.

import sys

# 32 bit integers
l = int()
sys.getsizeof(l)  # 24 bytes overhead; 144 bits
i = 5
hex(id(i))  # 0x955ea0
sys.getsizeof(i)  # 28 bytes; 24 bytes of overhead; 4 bytes (32 bits)

ls = []
sys.getsizeof(ls)  # 56 bytes

ls = [5]
hex(id(ls))  # 0x7f7c400ca500
hex(id(ls[0]))  # 0x955ea0

64 bytes for a list with 56 bytes overhead and one integer element...

This suggests an 8 byte, 64 bit integer (below):

sys.getsizeof(ls)  # 64 bytes; 56 bytes overhead; 8 bytes for single integer; 64 bit integer

However, element does point to a memory address of a 32-bit integer...

sys.getsizeof(ls[0])  # 28 bytes; 24 bytes overhead; 4 bytes for single integer; 32 bit integer

What are the mysterious 4 bytes?

IS IT THE ADDITIONAL SPACE ALLOCATED IN AN ARRAY FOR AN ADDITIONAL INTEGER?

nova24
  • 21
  • 6
  • `sys.getsizeof(ls[0])` returns the same as `sys.getsizeof(i)` - 28. There is no difference in size? – Iain Shelvington Jun 12 '22 at 23:57
  • I edited the original post a little bit; maybe it clear things up... – nova24 Jun 12 '22 at 23:59
  • Is the confusion caused by the size of a list increasing by 8 for each element? That's just the size of the pointer/reference to an object, if you add any arbitrary object to the list the size will increase by 8 each time – Iain Shelvington Jun 13 '22 at 00:03
  • Exactly; increasing by 8 bytes, but the elements in the list are only 4 bytes. – nova24 Jun 13 '22 at 00:05
  • 1
    The value returned when calling `sys.getsizeof` on a list does not include the size of the elements in the list, the 8 bytes are just the size of the pointer/reference to each element – Iain Shelvington Jun 13 '22 at 00:08
  • Just curious, could be a separate question, if the whole idea of pointers is to preserve memory, creating pointers that take up more memory than the value they reference seems to be counter intuitive... I guess when working with values greater than 32 bits it makes sense and in this case this example is too simple. – nova24 Jun 13 '22 at 00:24
  • Couple of things to address there. The int object referenced is 28 bytes which is larger than the pointer. Using pointers is not about preserving memory, it's a way of referencing arbitrary Python objects, where did you read that pointers are about saving memory? – Iain Shelvington Jun 13 '22 at 00:30
  • Duplicate of https://stackoverflow.com/questions/72512066/, but the point is that getsizeof is shallow, not recursive/deep. – Dennis Jun 13 '22 at 00:36
  • Memory Optimization portion of the following article: https://towardsdatascience.com/python-memory-and-objects-e7bec4a2845 ...maybe I misunderstood what I read. – nova24 Jun 13 '22 at 00:45
  • So to be clear, out of the 28 bytes of an integer, 24 is the overhead - meaning the methods associated with the int object in Python; but the stand alone value of an integer is 4 bytes, lets say an integer value of '2' (i.e., 0000 00010 0000 0000 0000 0000 0000 0000). Therefore, in Python, a value of int(2) would be stored across 28 bytes (28 memory slots), with binary representation of not only the number 2, but also all the methods associated with the int class? ...Therefore, the 8 byte pointers to the 28 byte integers are certainly effective. – nova24 Jun 13 '22 at 00:59
  • @nova24 Yes, there are 24 bytes of metadata stored for that int object. The metadata contains things like a pointer to the object's type and the objects ref count – Iain Shelvington Jun 13 '22 at 01:27
  • @IainShelvington thank you again; you really cleared up a major confusion on my end that I did not know I misunderstood earlier. – nova24 Jun 13 '22 at 02:09
  • Does this answer your question? [sys.getsizeof(list) returns less than the sum of its elements](https://stackoverflow.com/questions/30047388/sys-getsizeoflist-returns-less-than-the-sum-of-its-elements) – Edward Ji Jun 13 '22 at 06:22

1 Answers1

1

The value returned when calling sys.getsizeof on a list does not include the size of the elements in the list, the 8 bytes are just the size of the pointer/reference to each element – Iain Shelvington

nova24
  • 21
  • 6