使用Python 2.7.6在Windows上使用Guppy / Heapy

时间:2014-09-26 12:50:53

标签: python windows guppy heapy

我刚刚安装了孔雀鱼0.1.10。然后输入Ipython 2.1.0

from guppy import hpy
hp = hpy()

使控制台崩溃,即Windows告诉我,python.exe刚刚崩溃。有没有解决方法?还是一个很好的替代方案?

1 个答案:

答案 0 :(得分:3)

我和guppy / heapy有同样的问题。 Guppy目前有点过时了。有一些补丁但我再也找不到它了。然而它在python 2.7中对我没有用。由于操作系统拱门(64位!?),您也可能遇到问题。

关于内存分析技术还有其他问题:

我个人认为最有价值的替代方案是:

计算对象的大小并自行跟踪它也很有用。我详细阐述了一些代码(最初由https://stackoverflow.com/users/216356/noctis-skytower),我在其中一个StackOverflow问题(Approximately how much memory would a list of 80000 items consume in python?)中找到了与Python 2.7兼容的代码(也应该在3中工作):

totalSizeOf = lambda obj: sum(map(sys.getsizeof, explore(obj, set())))
def explore(obj, memo):
    loc = id(obj)
    if loc not in memo:
        memo.add(loc)
        yield obj
        # Handle instances with slots.
        try:
            slots = obj.__slots__
        except AttributeError:
            pass
        else:
            for name in slots:
                try:
                    attr = getattr(obj, name)
                except AttributeError:
                    pass
                else:
                    #yield from explore(attr, memo)
                    for bar in explore(attr, memo):
                        yield bar
        # Handle instances with dict.
        try:
            attrs = obj.__dict__
        except AttributeError:
            pass
        else:
            #yield from explore(attrs, memo)
            for bar in explore(attrs, memo):
                yield bar
        # Handle dicts or iterables.
        for name in 'keys', 'values', '__iter__':
            try:
                attr = getattr(obj, name)
            except AttributeError:
                pass
            else:
                for item in attr():
                    #yield from explore(item, memo)
                    for bar in explore(item, memo):
                        yield bar