python-列出内存使用情况

我正在尝试改善python中脚本的内存使用率,因此我需要知道列表中的RAM使用率是多少.
我用以下方法测量内存使用量
print str(sys.getsizeof(my_list)/1024/1024)
希望可以给我提供以MB为单位的RAM中列表的大小.
它输出12 Mb,但是在顶部命令中,我看到我的脚本在运行时使用了4G笔记本电脑70%的RAM.
此外,此列表应包含?500Mb文件中的内容.
因此12Mb是不现实的.
如何测量实际内存使用量?
解决方法:
sys.getsizeof仅考虑列表本身,而不考虑列表中包含的项目.
根据sys.getsizeof documentation:
…
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