如何测量python中对象的内存使用情况? [英] How do I measure the memory usage of an object in python?

查看:216
本文介绍了如何测量python中对象的内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的python类foo:

I have a python class foo that contains:

  • 数据(整数,浮点数)
  • (整数,浮点数和其他对象的)列表
  • (整数,浮点数或其他对象的)字典

假设没有反向引用(循环),是否有一种简单的方法来测量foo对象的总内存使用情况?

Assuming that there are no back-references (cycles), is there an easy way to measure the total memory usage of a foo object ?

本质上,我正在寻找sys.getsizeof

我遇到的一些工具包括:笨重的,objgraph和gc,但我认为它们都不适合完成任务(对此我可能会得到纠正)

A few of the tools I came across included: heapy, objgraph and gc, but I don't think any of them are up to the task (I may be corrected on this)

推荐答案

尝试 Pympler

类似

>>> import pympler
>>> print pympler.asizeof.asizeof(your_object)

过去对我有帮助.

您可以搜索SO 了解更多信息-这是

You can search S.O. for more info - here's a recommendation from Alex Martelli.

这篇关于如何测量python中对象的内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆