Python 数据结构内存占用是原始数据的 5 倍左右

2021-02-05 14:38:08 +08:00
 wohenfuyou

大概是如下结构体
[{dic1....},{key1:[value1,value2...],key2:[value1,value2...]},{dic3},{dic4}]

1.5g 数据要 7g 多内存,请问这是正常的内存占用么,要是优化的话在哪方面优化呢,请教各位~

2721 次点击
所在节点    Python
12 条回复
linksNoFound
2021-02-05 14:59:05 +08:00
买内存
lkytal
2021-02-05 15:00:17 +08:00
不用 Python (雾
est
2021-02-05 15:18:13 +08:00
一般是用 __slots__ 缓解。不过你遇到内存问题建议 numpy 。
iqxd
2021-02-05 15:29:43 +08:00
大量小数据如果纠结内存就不要用通用数据结构。
为了读取和插入效率,python 的 dict 的默认分配键值空间与实际键值数量大概是 3:1.
对于 python 还算正常,即使是 C 的双向链表,用 64 位指针有时候占用内存都可能是实际数据的 3 倍
ebingtel
2021-02-05 15:30:12 +08:00
纯粹遍历的话 用生成器 别用 list
gjquoiai
2021-02-05 16:49:17 +08:00
pandas 解君愁
tmackan
2021-02-05 18:00:47 +08:00
py 也有基础类型的,除了 list,tuple,dict 以后,也支持特定类型的类型,可以看下,比如只支持 int 的 list
tmackan
2021-02-05 18:14:22 +08:00
@tmackan 纠正下 from array import array 这个数组,只能指定特定类型的数组

A Python array is as lean as a C array. When creating an array, you provide a typecode, a letter to determine the underlying C type used to store each item in the array. For example, b is the typecode for signed char. If you create an array('b'), then each item will be stored in a single byte and interpreted as an integer from –128 to 127. For large sequences of numbers, this saves a lot of memory. And Python will not let you put any number that does not match the type for the array.
vance123
2021-02-05 18:18:27 +08:00
虽然 Python 提供了比 C 更多的功能, 但这是以时间和空间为代价的. 以 str 为例, 单个对象起步就是几十 Byte 的 metadata, 字符串中只要有一个中文字符, 其内存占用都会比纯 ascii 多上 3 倍.
lysS
2021-02-05 18:59:01 +08:00
py 又不是 C,是这样的,我记得 PHP 的数组占用大小也是实际大小的 4 倍左右
littleMaple
2021-02-06 02:17:12 +08:00
感觉是个 XY 问题,题主如果可以给出更多上下文,可以更好地解决。

如果是 homogeneous data structure,可以用楼上说的 numpy,pandas,array 等库。

另外可以考虑转换成数据库数据,需要访问数据时再和数据库进行交互,就不必把所有数据都一下子挤进内存中。顺带一提 Python 有原生自带数据库交互库叫做 sqlite3.

看你列出的数据的格式,似乎像是 JSON,可以用一些 lazy-parse json 的库,例如 ijson ( https://pypi.org/project/ijson/), json-streamer ( https://github.com/kashifrazzaqui/json-streamer), bigjson ( https://github.com/henu/bigjson), jsonslicer ( https://pypi.org/project/jsonslicer/).

如果对精度要求不高,可以用一些 succinct data structure 来大大简化数据结构空间开销,例如布隆过滤器,HyperLogLog,Count-Min sketch 等。

关于数据空间占用的优化,可以看经典的《 Python 高性能编程》,里面除了讲 Python 代码如何提升时间性能,还讲了 Python 代码如何提升空间性能,讲的非常全面细致,大部分可用的选项都覆盖了.
hotea
2021-02-07 20:18:06 +08:00
数据有重复值的话 categorical 可以省一点内存
https://pandas.pydata.org/pandas-docs/stable/user_guide/categorical.html

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

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

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

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

© 2021 V2EX