如何在ZODB中查找大对象

时间:2014-11-17 16:26:12

标签: zope zodb

我试图分析我的ZODB,因为它变得非常大(包装后它也很大)。

zodbbrowser有一个feature,用于显示对象的字节数。它是通过获取腌制状态的长度(变量的名称)来实现的,但它也有一些我不能完全理解的魔法。

我如何在ZODB中找到最大的物体?

1 个答案:

答案 0 :(得分:0)

我已经写了一个应该做到这一点的方法。随意使用它,但要注意这是非常耗费内存的。必须安装包zodbbrowser

def zodb_objects_by_size(self):
    """
        Recurse over the ZODB tree starting from self.aq_parent. For
        each object, use zodbbrowser's implementation to get the raw
        object state. Put each length into a Counter object and
        return a list of the biggest objects, specified by path and
        size.
    """
    from zodbbrowser.history import ZodbObjectHistory
    from collections import Counter

    def recurse(obj, results):
        # Retrieve state pickle from ZODB, get length
        history = ZodbObjectHistory(obj)
        pstate = history.loadStatePickle()
        length = len(pstate)

        # Add length to Counter instance
        path = '/'.join(obj.getPhysicalPath())
        results[path] = length

        # Recursion
        for child in obj.contentValues():

            # Play around portal tools and other weird objects which
            # seem to contain themselves
            if child.contentValues() == obj.contentValues():
                continue

            # Rolling in the deep
            try:
                recurse(child, results)
            except (RuntimeError, AttributeError), err:
                import pdb; pdb.set_trace()  ## go debug

    results = Counter()
    recurse(self.aq_parent, results)
    return results.most_common()