Unexpected Size of Python Objects in Memory Last Updated : 28 Nov, 2021 Comments Improve Suggest changes Like Article Like Report 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. Syntax: sys.getsizeof(object)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. Example: 'e' will consume - 50 'eo' will consume - 51 'eoo' will consume - 52 Python3 # import sys module import sys # get the size of the given string print(sys.getsizeof('g')) # get the size of the given string print(sys.getsizeof('ge')) # get the size of the given string print(sys.getsizeof('gee')) # get the size of the given string print(sys.getsizeof('geek')) # get the size of the given string print(sys.getsizeof('geeks')) Output: 50 51 52 53 54Example 2: Python program to get the unexpected size of integer object Integer object will take 28 bytes Python3 # import sys module import sys # get the size of the given integer print(sys.getsizeof(123)) # get the size of the given integer print(sys.getsizeof(21)) Output: 28 28Example 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 [1] - 72 + 8 = 80 [1,2] - 72 +8 + 8 =88 Python3 # import sys module import sys # get the size of empty list print(sys.getsizeof([])) # get the size of list with one element print(sys.getsizeof([2])) # get the size of list with two elements print(sys.getsizeof([22, 33])) Output: 72 80 88Example 4: Get unexpected size of the dictionary object This object will consume 248 bytes irrespective of the number of items. Python3 # import sys module import sys # get the size of empty dictionary print(sys.getsizeof({})) # get the size of dictionarydictionary with one element print(sys.getsizeof({'k': 2})) # get the size of list with two elements print(sys.getsizeof({'k': 2, 'h': 45})) Output: 248 248 248 Comment More infoAdvertise with us Next Article Unexpected Size of Python Objects in Memory S sravankumar_171fa07058 Follow Improve Article Tags : Python Python-sys Practice Tags : python Similar Reads How to find size of an object in Python? In python, the usage of sys.getsizeof() can be done to find the storage size of a particular object that occupies some space in the memory. This function returns the size of the object in bytes. It takes at most two arguments i.e Object itself. Note: Only the memory consumption directly attributed t 2 min read How to get the memory address of an object in Python In Python, everything is an object, from variables to lists and dictionaries everything is treated as objects. In this article, we are going to get the memory address of an object in Python. Method 1: Using id() We can get an address using the id() function. id() function gives the address of the pa 2 min read Releasing Memory in Python Python's memory management is primarily handled by its built-in garbage collector (GC), which automatically deallocates memory that is no longer in use. However, to optimize memory usage, developers can employ explicit techniques to manage memory more effectively, especially in long-running or memor 4 min read Memory Leak in Python requests When a programmer forgets to clear a memory allocated in heap memory, the memory leak occurs. It's a type of resource leak or wastage. When there is a memory leak in the application, the memory of the machine gets filled and slows down the performance of the machine. This is a serious issue while bu 5 min read Memory profiling in Python using memory_profiler If you use Python a lot then you probably know that many people claim that Python takes up more time to execute. Well, you probably have seen approaches like the total time spent to execute a part of code or something like that but sometimes you need something more than that. What about RAM usage? N 3 min read Like