列出内存使用情况 [英] List memory usage

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

问题描述

我正在尝试提高python中脚本的内存使用率,因此我需要知道列表中的RAM使用率是多少. 我用

I am trying to improve the memory usage of my script in python, therefore I need to know what's RAM usage of my list. I measure the memory usage with

print str(sys.getsizeof(my_list)/1024/1024)

希望这会给我RAM中以MB为单位的列表大小.

which hopefully would give me the size of the list in RAM in Mb.

它输出12 Mb,但是在top命令中,我看到我的脚本在运行时使用了4G笔记本电脑70%的RAM.

it outputs 12 Mb, however in top command I see that my script uses 70% of RAM of 4G laptop when running.

此外,此列表应包含〜500Mb文件中的内容.

In addition this list should contain a content from file of ~500Mb.

所以12Mb是不现实的.

So 12Mb is unrealistic.

如何测量实际内存使用量?

How can I measure the real memory usage?

推荐答案

sys.getsizeof仅考虑列表本身,而不考虑列表中包含的项目.

sys.getsizeof only take account of the list itself, not items it contains.

根据 sys.getsizeof文档:

... 只有直接归因于该对象的内存消耗是 而不是它所引用的对象的内存消耗. ...

... Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to. ...

使用 Pympler :

>>> import sys
>>> from pympler.asizeof import asizeof
>>>
>>> obj = [1, 2, (3, 4), 'text']
>>> sys.getsizeof(obj)
48
>>> asizeof(obj)
176

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

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