如何可以得知,运行的某个时候,产生的目标字典占用了多少内存?

2020-01-15 20:44:07 +08:00
 qazwsxkevin
比如想得知,在函数运行后,return 回来的 dict 占了多少 MB 内存这样?
2417 次点击
所在节点    Python
6 条回复
casparchen
2020-01-15 20:46:56 +08:00
你这个标题对仗不整齐啊,逗号少一个。

如何才可以得知,
运行的某个时候,
产生的目标字典,
占用了多少内存。

看看这样是不是好多了 ?
si
2020-01-15 20:51:18 +08:00
我搜索了一下:
Python 获取对象所占内存大小方法:
sys.getsizeof()
内置函数__sizeof__()
iamwho
2020-01-15 20:51:53 +08:00
sys.getsizeof()

?
qazwsxkevin
2020-01-15 22:59:53 +08:00
@casparchen 最后一个逗号可以不必吧。。。

@si 我第一认知也是这个,只是这个似乎是不准确。。。

@iamwho 这个似乎是不准确的。。。

看了一晚网上关于这方面的各种说法,问的这个问题,似乎在 Python 上无解。。。
不过不重要了,打算把磁盘上 53MB 的内容放入字典传来传去了
iamwho
2020-01-16 00:32:13 +08:00
@qazwsxkevin #4

Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.

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

https://docs.python.org/3/library/sys.html#sys.getsizeof
yucongo
2020-01-18 12:25:39 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/638246

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX