
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding Memory Usage of an Object in Python
Memory is frequently set up in a computer system as a series of binary digits, or bits. Each byte is given a unique memory location that may be used to read from or write to the byte's value. Bytes, which can be interpreted as characters, integers, floating-point numbers, or other data kinds, are used to store data in memory. You may measure memory use in Python with the aid of tools like the built-in sys.getsizeof() and asizeof() from pympler.
Method 1: Using Getsizeof() Function
The amount of memory required by an object or data structure may be ascertained with this function. The object whose memory size has to be measured serves as the only argument. Any sort of object, including classes and instances specified by the user, may be passed as argument.
Syntax
sys.getsizeof(*objt_name*)
*objt_name* can take any object - integer, strings, functions, etc.
The function returns the size of the given object in bytes.
Algorithm
Import the required sys module.
Create a dictionary with various data types, such as strings, integers, floats, lists, and functions.
Create a function that takes the integer n as an input and returns a list of integers ranging from 0 to n-1.
Use sys.getsizeof() to find the size of the methods create_nmbr_list and my_dictn.
Print the value.
Time Complexity ? O(1)
Space Complexity ? O(n) where n is the size of the dictionary.
The following example illustrates creating a dictionary that includes a list and a lambda function among other data types. A function that generates a lengthy list of integers using the getsizeof() function.
Example
import sys # Define a dictionary with various data types my_dictn = { "string": "Tutorials Point", "integer": 662, "float": 3.14, "list": [12, 22, 32, 42, 52], "function": lambda x: x ** 2 } # Define a function that creates list of integers def create_nmbr_list(n): return [i for i in range(n)] # Measure the size of the dictionary and function using sys.getsizeof() print("Size of dictionary using sys.getsizeof(): ", sys.getsizeof(my_dictn)) print("Size of function using sys.getsizeof(): ", sys.getsizeof(create_nmbr_list))
Output
Size of dictionary using sys.getsizeof(): 232 Size of function using sys.getsizeof(): 144
Note ? The size of the items in memory might change depending on a variety of factors, including the amount of memory presently being utilised by other programmes or the operating system.
Method 2: Using Asizeof( ) Function
This function is provided by the Pympler package and used for memory profiling and optimization. Unlike the built-in getsizeof() function, asizeof() takes into account the memory usage of all the objects referenced by the target object.
Syntax
asizeof(*object_*)
*object_* can take any object - integers, strings, functions, boolean, etc
The function returns the size of the given object and its references in bytes.
Algorithm
Import the required pympler.asizeof() module.
Create a dictionary called my_dict2 with different data types.
Create the function to produce a list of integers.
My_dict2 and create_int_list's sizes can be recorded, using the asizeof function.
Using the variables dictn_size_pympler and fnct_size_pympler, print the sizes of my_dict2 and create_int_list.
Time Complexity ? O(1)
Space Complexity ? O(n) where n is the size of the dictionary.
The following program illustrates a dictionary with lambda function and other data types using asizeof function to find the memory used by the object.
Example
from pympler.asizeof import asizeof # Define a dictionary with various data types my_dict2 = { "string": "Hello Viewers", "integer": 42, "float": 3.14, "list": [1, 2, 3, 4, 5], "set": {10, 20, 30, 40, 50}, "boolean": True, "function": lambda x: x ** 2, } # Define a function that creates a large list of integers def create_int_list(i): return [x for x in range(i)] # Measure the size of the dictionary and function using pympler.asizeof() dictn_size_pympler = asizeof(my_dict2) fnct_size_pympler = asizeof(create_int_list) # Print the results print("Size of dictionary using pympler.asizeof: ", dictn_size_pympler) print("Size of function using pympler.asizeof: ", fnct_size_pympler)
Output
Size of dictionary using pympler.asizeof: 384 Size of function using pympler.asizeof: 152
Conclusion
Sys.getsizeof() just returns the size of the item itself, whereas asizeof() returns the size of the object plus every object it references. As a result, asizeof() can show the memory utilisation of an object more clearly.
While sys.getsizeof() just returns the size of the object itself, asizeof() returns the size of every module that the programme has imported and utilised. Depending on the version of Python being used, sys.getsizeof() may react differently.