Is there a memory profiler for python2.7?

Question:

I needed to check the memory stats of objects I use in python.
I came across guppy and pysizer, but they are not available for python2.7.
Is there a memory profiler available for python 2.7?
If not is there a way I can do it myself?

Asked By: simha

||

Answers:

I’m not aware of any profilers for Python 2.7 — but check out the following function which has been added to the sys module, it could help you do it yourself.

“A new function, getsizeof(), takes a
Python object and returns the amount
of memory used by the object, measured
in bytes. Built-in objects return
correct results; third-party
extensions may not, but can define a
__sizeof__() method to return the object’s size.”

Here’s links to places in the online docs with information about it:

    What’s New in Python 2.6
    27.1. sys module — System-specific parameters and functions

Answered By: martineau

You might want to try adapting the following code to your specific situation and support your data types:

import sys


def sizeof(variable):
    def _sizeof(obj, memo):
        address = id(obj)
        if address in memo:
            return 0
        memo.add(address)
        total = sys.getsizeof(obj)
        if obj is None:
            pass
        elif isinstance(obj, (int, float, complex)):
            pass
        elif isinstance(obj, (list, tuple, range)):
            if isinstance(obj, (list, tuple)):
                total += sum(_sizeof(item, memo) for item in obj)
        elif isinstance(obj, str):
            pass
        elif isinstance(obj, (bytes, bytearray, memoryview)):
            if isinstance(obj, memoryview):
                total += _sizeof(obj.obj, memo)
        elif isinstance(obj, (set, frozenset)):
            total += sum(_sizeof(item, memo) for item in obj)
        elif isinstance(obj, dict):
            total += sum(_sizeof(key, memo) + _sizeof(value, memo)
                         for key, value in obj.items())
        elif hasattr(obj, '__slots__'):
            for name in obj.__slots__:
                total += _sizeof(getattr(obj, name, obj), memo)
        elif hasattr(obj, '__dict__'):
            total += _sizeof(obj.__dict__, memo)
        else:
            raise TypeError('could not get size of {!r}'.format(obj))
        return total
    return _sizeof(variable, set())
Answered By: Noctis Skytower

Here’s one that works for Python 2.7: The Pympler package.

Answered By: partofthething

pympler 0.9 is the latest version that supports Python 2.7, see https://github.com/pympler/pympler/tags or just use pip

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.