且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

cProfile占用大量内存

更新时间:2023-02-03 11:58:32

已更新:由于cProfile已内置在当前版本的Python(_lsprof扩展名)中,因此应使用主分配器.如果这对您不起作用,则Python 2.7.1具有--with-valgrind编译器选项,该选项会导致它在运行时切换为使用malloc().很好,因为它避免了使用禁止文件.您可以构建一个仅用于概要分析的版本,然后在valgrind下运行Python应用程序,以查看分析器所做的所有分配以及使用自定义分配方案的所有C扩展.

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):

也许尝试查看分配的去向.如果您在代码中有一个位置可以定期转储内存使用量,则可以使用 guppy 查看分配:

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())