Unexpected Size of Python Objects in Memory
In this article, we will discuss unexpected size of python objects in Memory.
Python Objects include List, tuple, Dictionary, etc have different memory sizes and also each object will have a different memory address. Unexpected size means the memory size which we can not expect. But we can get the size by using getsizeof() function. This will return the memory size of the python object. This function is available in the sys module so we need to import it.
Example 1: Python code to get the unexpected size of string object
A string with a single character will consume 50 bytes, then after 1 character, it will consume n character bytes.
‘e’ will consume – 50
‘eo’ will consume – 51
‘eoo’ will consume – 52
50 51 52 53 54
Example 2: Python program to get the unexpected size of integer object
Integer object will take 28 bytes
Example 3: Python code to get unexpected size of list object
We can define the list as . An empty list will consume 72 bytes and consume extra 8 bytes for each element.
 – 72
 – 72 + 8 = 80
[1,2] – 72 +8 + 8 =88
72 80 88
Example 4: Get unexpected size of the dictionary object
This object will consume 248 bytes irrespective of the number of items.
248 248 248