Python 深度 getsizeof 列表与内容? [英] Python deep getsizeof list with contents?

查看:35
本文介绍了Python 深度 getsizeof 列表与内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶 sys.getsizeof( 10000*[x] )是 40036,与 x 无关:0, "a", 1000*"a", {}.
有没有deep_getsizeof哪个适当地考虑了共享内存的元素?
(问题来自查看内存数据库表,例如range(1000000) -> 省份名称:列表还是字典?)
(Mac ppc 上的 Python 是 2.6.4.)

I was surprised that sys.getsizeof( 10000*[x] ) is 40036 regardless of x: 0, "a", 1000*"a", {}.
Is there a deep_getsizeof which properly considers elements that share memory ?
(The question came from looking at in-memory database tables like range(1000000) -> province names: list or dict ?)
(Python is 2.6.4 on a mac ppc.)

补充:10000*["Mississippi"] 是 10000 个指向一个 "Mississippi" 的指针,正如一些人所指出的那样.试试这个:

Added: 10000*["Mississippi"] is 10000 pointers to one "Mississippi", as several people have pointed out. Try this:

nstates = [AlabamatoWyoming() for j in xrange(N)]

其中 AlabamatoWyoming() -> 字符串Alabama"..Wyoming".什么是 deep_getsizeof(nstates) ?
(我们怎么知道?

where AlabamatoWyoming() -> a string "Alabama" .. "Wyoming". What's deep_getsizeof(nstates) ?
(How can we tell ?

  • 适当的 deep_getsizeof:困难,~gc 跟踪器
  • 从总 vm 估计
  • python 实现的内部知识
  • 猜测.

于 25 月添加:另见 when-does-python-allocate-new-memory-for-identical-strings

Added 25jan: see also when-does-python-allocate-new-memory-for-identical-strings

推荐答案

看看 guppy/heapy;我自己并没有过多地使用它,但我的一些同事已经将它用于内存分析并取得了良好的效果.

Have a look at guppy/heapy; I haven't played around with it too much myself, but a few of my co-workers have used it for memory profiling with good results.

文档可能会更好,但是这个方法很好地解释了基本概念.

The documentation could be better, but this howto does a decent job of explaining the basic concepts.

这篇关于Python 深度 getsizeof 列表与内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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