Find out how much memory is being used by an object in Python

dwestbrook picture dwestbrook · Aug 29, 2008 · Viewed 166k times · Source

How would you go about finding out how much memory is being used by an object? I know it is possible to find out how much is used by a block of code, but not by an instantiated object (anytime during its life), which is what I want.

Answer

Uzer picture Uzer · Nov 8, 2013

Try this:

sys.getsizeof(object)

getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.

A recursive recipe