cProfile taking a lot of memory

2019-08-05 04:15发布

问题:

I am attempting to profile my project in python, but I am running out of memory.

My project itself is fairly memory intensive, but even half-size runs are dieing with "MemoryError" when run under cProfile.

Doing smaller runs is not a good option, because we suspect that the run time is scaling super-linearly, and we are trying to discover which functions are dominating during large runs.

Why is cProfile taking so much memory? Can I make it take less? Is this normal?

回答1:

Updated: Since cProfile is built into current versions of Python (the _lsprof extension) it should be using the main allocator. If this doesn't work for you, Python 2.7.1 has a --with-valgrind compiler option which causes it to switch to using malloc() at runtime. This is nice since it avoids having to use a suppressions file. You can build a version just for profiling, and then run your Python app under valgrind to look at all allocations made by the profiler as well as any C extensions which use custom allocation schemes.

(Rest of original answer follows):

Maybe try to see where the allocations are going. If you have a place in your code where you can periodically dump out the memory usage, you can use guppy to view the allocations:

import lxml.html
from guppy import hpy

hp = hpy()
trees = {}
for i in range(10):
    # do something
    trees[i] = lxml.html.fromstring("<html>")
    print hp.heap()

    # examine allocations for specific objects you suspect
    print hp.iso(*trees.values())