profiling.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:pykit 作者: baishancloud 项目源码 文件源码
def _memory_dump(opts):

    for typ, n in objgraph.most_common_types():
        logging.info('{typ:30} {n:>10}'.format(typ=typ, n=n))

    objects = []
    rng = opts['size_range']

    summ = {
        'max_refsize': {
            'size': 0,
        },
    }

    for obj in gc.get_objects():

        if not hasattr(obj, '__class__'):
            continue

        size = sys.getsizeof(obj, 0)

        if rng is not None:
            if not (rng[0] <= size < rng[1]):
                continue

        i = id(obj)

        # referrers = [id(o)
        #              for o in gc.get_referrers(obj)
        #              if hasattr(o, '__class__')]

        referents = [(id(o), _get_class(o), sys.getsizeof(o, 0))
                     for o in gc.get_referents(obj)
                     # if hasattr(o, '__class__')
                     ]

        refsize = sum([x[2] for x in referents])

        cls = _get_class(obj)

        data = [
            i,
            cls,
            size,           # object size
            refsize,        # size of all direct referents
            referents,      # referents
        ]

        objects.append(data)

        if summ['max_refsize']['size'] < refsize:

            summ['max_refsize'] = {
                'size': refsize,
                'object': data,
            }

    for o in objects:
        logging.info('memory-dump: ' + json.dumps(o))

    logging.info('memory-dump summary: ' + json.dumps(summ))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号