personal web log written by izabeera and dryobates

python django memory_profiler

Memory profiler with django templatetags

by dryobates

Debugging memory leaks in python is quite difficult task. Python module: memory_profiler makes it a little easier task.

Today I’ve found interesting python module: memory_profiler [1].

It’s quite useful if you know that memory leaks in some particularly function. After decorating function and running program you got source code annotated line by line with memory usage. However there’s a little bug in it. Decorator @profile doesn’t use functools.update_wrapper, so for example you can’t use it directly to measure django templatetag like that:

1
2
3
4
5
6
7
 from memory_profiler import profile


 @register.inclusion_tag('dummy.html')
 @profile
 def my_template_tag():
     return {}

You have to change it to something like that instead:

1
2
3
4
5
6
7
 from memory_profiler import profile


 def my_template_tag():
     return {}
 my_template_tag = functools.update_wrapper(profile(my_template_tag), my_template_tag)
 register.inclusion_tag('dummy.html')(my_template_tag)

Module uses psutil [2] for checking memory usage. It checks memory usage by process id. In multi threaded environments you can sometimes get false measurements. Other thread could eat memory when you measure your suspected function. Nonetheless it’s good to have such tool in your toolbox.

[1]Memory Profiler - python module for monitoring memory usage https://pypi.python.org/pypi/memory_profiler
[2]psutil - python module providing interface for retrieving information on running processes https://pypi.python.org/pypi/psutil/
dryobates
dryobates
Jakub Stolarski. Software engineer. I work professionally as programmer since 2005. Speeding up software development with Test Driven Development, task automation and optimization for performance are things that focus my mind from my early career up to now. If you ask me for my religion: Python, Vim and FreeBSD are my trinity ;) Email: jakub@stolarscy.com

Archive

Tag cloud