5

My code allocates continously memory (~12kb per second). With a runtime of 8 hours its a lot of memory!!

Because of this I want to trace the moments/code lines my python code allocates memory.

Something like you can do with processed code lines with:

python -m trace --count -C ./tmp code.py

this generate a view where you can see how often this line was executed. It looks like:

code.cover

1:     import sys
1:     import os
1534:  while 1:
1534:      print "foo"

I need this for memory allocation. if possible something like

1245 B    import sys
893 B     import os
17.46 KB  import somecode
2
  • Did you try using valgrind ? In special, check the massif tool that comes with valgrind. Commented Jan 2, 2013 at 15:23
  • possible duplicate of Which Python memory profiler is recommended? Commented Jan 12, 2014 at 16:15

1 Answer 1

3

Looks like this question has already been answered here: Python memory profiler

Maybe this one can help you: http://pypi.python.org/pypi/memory_profiler

From the docs, execute the code passing the option -m memory_profiler to the python interpreter to load the memory_profiler module and print to stdout the line-by-line analysis. If the file name was example.py, this would result in:

$ python -m memory_profiler example.py

Output will follow:

Line #    Mem usage  Increment   Line Contents
==============================================
     3                           @profile
     4      5.97 MB    0.00 MB   def my_func():
     5     13.61 MB    7.64 MB       a = [1] * (10 ** 6)
     6    166.20 MB  152.59 MB       b = [2] * (2 * 10 ** 7)
     7     13.61 MB -152.59 MB       del b
     8     13.61 MB    0.00 MB       return a
Sign up to request clarification or add additional context in comments.

2 Comments

This is very helpfull, but i need a comparison of my memory usage, because there are many threaded loops. So that if i exit the code, i see that during the runtime this function allocated 25 MB memory
Have you noticed the link to a similar question? There are far more tools there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.