How To Get The Memory Usage Of An Object In Python

Get the memory usage of an Object in Python

To get the memory usage of an object in Python, we have tested it effectively with three methods. Check out the details below to get more information.

Get the memory usage of an object in Python

Using sys.getsizeof()

This method can be used when you have imported the library sys in Python. To do this, please type the command “import sys” at the head of your program.

Syntax:

sys.getsizeof(object)

Parameter: 

  • object: is the object of which you want to get the size.

Return value: return value is the number of memory used in an object.

Code example:

Firstly we must import sys. Then create some objects of different types, such as a list or a string. Secondly, we print out the result of this method, which means the memory usage of these objects:

import sys

myList = [1, 2, 3]
myString = "LEARNSHAREIT"

# Get memory usage of myList
print(sys.getsizeof(myList))

# Get memory usage of myString
print(sys.getsizeof(myString))

Output

80
61

Another example:

In this example, we will get the size of an object which is of type of a defined class named Animals.

import sys

# Create the Animals class has 3 attributes
class Animals:
    monkey = 1
    bird = 2
    whale = 3
    
myBird = Animals()

# Get memory usage of myBird
print(sys.getsizeof(myBird))

Output

48

This solution worked for the Animals objects directly. However, when it comes to a more complex class with many methods and different objects, this method cannot be used to get the memory size anymore. To do this, please check out the next solution.

Using asizeof.asizeof()

Another simple way to get the memory usage of an object is using asizeof.asizeof(). We will have to install Pympler in Python (reading instructions here) and then import the library asizeof.

Syntax:

asizeof.asizeof(object)

Parameter: 

  • object: is the object of which you want to get the size.

Return value: return value is the number of memory used in an object.

Example:

from pympler import asizeof

myList = [1, 2, 3]
myString = "LEARNSHAREIT"

# Get memory usage of myList
print(asizeof.asizeof(myList))

# Get memory usage of myString
print(asizeof.asizeof(myString))

Output:

184
64

The asizeof.asizeof function of pympler can be used to estimate the memory used by specific Python objects. By comparison with sys.getsizeof, asizeof only gets the size of the objects recursively. Users can access this functionality through the asizeof functions to get a comprehensive list of referents and their corresponding memory size.

Using pickle.dumps()

This is the relative way to get the memory size of an object using  pickle.dumps(). We will have to import the library pickle to serialize the object:

Syntax

pickle.dumps(object)

Parameter: 

  • object: is the object of which you want to get the size.

Return value: return a relative memory usage in an object.

Example:

import pickle

myList = [1, 2, 3]
myString = "LEARNSHAREIT"

# Get memory usage of myList
print(len(pickle.dumps(myList)))

# Get memory usage of myString
print(len(pickle.dumps(myString)))

Output

22
27

Memory size is a relative thing when it comes to Python. However, this method returns a result nearly close to an object’s size.

Summary

In this tutorial, we have explained how to get the memory usage of an object in Python by using three methods. We always hope this tutorial is helpful to you. If you have questions or comments about improving our articles, leave your comment here. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *