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?
Try Python profiler mprof for memory usage mprof will automatically create a graph of your script's memory usage over time, which you can view by running mprof plot. It's important to note here that plotting requires matplotlib. This is helpful when determining Python profile memory usage.
The line-by-line memory usage mode is used much in the same way of the line_profiler: first decorate the function you would like to profile with @profile and then run the script with a special script (in this case with specific arguments to the Python interpreter).
The Memory Profiler is a component in the Android Profiler that helps you identify memory leaks and memory churn that can lead to stutter, freezes, and even app crashes. It shows a realtime graph of your app's memory use and lets you capture a heap dump, force garbage collections, and track memory allocations.
How Memory Profiling for C and C++ Works. When an application node is executed, the source code is instrumented by the C or C++ Instrumentor (attolcpp or attolcc1). The resulting source code is then executed and the Memory Profiling feature outputs a static . tsf file for each instrumented source file and a dynamic .
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())
Here's one that works for Python 2.7: The Pympler package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With