Categories
Python Answers

How to profile memory usage in Python?

Spread the love

Sometimes, we want to profile memory usage in Python.

In this article, we’ll look at how to profile memory usage in Python.

How to profile memory usage in Python?

To profile memory usage in Python, we can use the guppy module.

For instance, we write:

from guppy import hpy

h = hpy()
heap = h.heap()
print(heap)

We call hpy to return an object with the heap method.

heap returns a string with the memory usage data in a string.

Therefore, heap is something like:

Partition of a set of 35781 objects. Total size = 4143541 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  10581  30   946824  23    946824  23 str
     1   7115  20   494688  12   1441512  35 tuple
     2   2534   7   447560  11   1889072  46 types.CodeType
     3   5001  14   354149   9   2243221  54 bytes
     4    449   1   349104   8   2592325  63 type
     5   2337   7   317832   8   2910157  70 function
     6    449   1   245120   6   3155277  76 dict of type
     7    101   0   179024   4   3334301  80 dict of module
     8    264   1   112296   3   3446597  83 dict (no owner)
     9   1101   3    79272   2   3525869  85 types.WrapperDescriptorType
<121 more rows. Type e.g. '_.more' to view.>

Size and cumulative are memory usage in bytes.

Conclusion

To profile memory usage in Python, we can use the guppy module.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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