Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python line-by-line memory profiler?

I'm looking to generate, from a large Python codebase, a summary of heap usage or memory allocations over the course of a function's run.

I'm familiar with heapy, and it's served me well for taking "snapshots" of the heap at particular points in my code, but I've found it difficult to generate a "memory-over-time" summary with it. I've also played with line_profiler, but that works with run time, not memory.

My fallback right now is Valgrind with massif, but that lacks a lot of the contextual Python information that both Heapy and line_profiler give. Is there some sort of combination of the latter two that give a sense of memory usage or heap growth over the execution span of a Python program?

like image 352
Tim Avatar asked Aug 09 '11 16:08

Tim


People also ask

How do I use memory profiler in Python?

The easiest way to profile a single method or function is the open source memory-profiler package. It's similar to line_profiler , which I've written about before . You can use it by putting the @profile decorator around any function or method and running python -m memory_profiler myscript.

What is memory profiler in Python explain how the Profiler performs a line-by-line analysis of memory?

Memory Profiler is a pure Python module that uses the psutil module. It monitors the memory consumption of a Python job process. Also, it performs a line-by-line analysis of the memory consumption of the application. The line-by-line memory usage mode works in the same way as the line_profiler.


2 Answers

I would use sys.settrace at program startup to register a custom tracer function. The custom_trace_function will be called for each line of code. Then you can use that function to store information gathered by heapy or meliae in a file for later processing.

Here is a very simple example which logs the output of hpy.heap() each second to a plain text file:

import sys import time import atexit from guppy import hpy  _last_log_time = time.time() _logfile = open('logfile.txt', 'w')  def heapy_profile(frame, event, arg):     currtime = time.time()     if currtime - _last_log_time < 1:         return     _last_log_time = currtime     code = frame.f_code     filename = code.co_filename     lineno = code.co_firstlineno     idset = hpy().heap()     logfile.write('%s %s:%s\n%s\n\n' % (currtime, filename, lineno, idset))     logfile.flush()  atexit.register(_logfile.close) sys.settrace(heapy_profile) 
like image 164
gurney alex Avatar answered Oct 11 '22 17:10

gurney alex


You might be interested by memory_profiler.

like image 41
gaborous Avatar answered Oct 11 '22 17:10

gaborous