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 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 Python | Pandas Series.memory_usage() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.memory_usage() function retur 2 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 Find out how much memory is being used by an object in Python In this article, we are going to see how to find out how much memory is being used by an object in Python. For this, we will use sys.getsizeof() function 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 i 2 min read Python | Pandas Index.memory_usage() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.memory_usage() function return the memory usage of the Index. It returns 2 min read Tips to reduce Python object size We all know a very common drawback of Python when compared to programming languages such as C or C++. It is significantly slower and isn't quite suitable to perform memory-intensive tasks as Python objects consume a lot of memory. This can result in memory problems when dealing with certain tasks. W 4 min read Object Interning In Python Object interning is a technique used in Python to optimize memory usage and improve performance by reusing immutable objects instead of creating new instances. It is particularly useful for strings, integers and user-defined objects. By interning objects, Python can store only one copy of each disti 3 min read Like