Reputation: 899
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?
Upvotes: 4
Views: 4965
Reputation: 154
pympler 0.9 is the latest version that supports Python 2.7, see https://github.com/pympler/pympler/tags or just use pip
Upvotes: 0
Reputation: 22041
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())
Upvotes: 2
Reputation: 123541
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
Upvotes: 1