简体   繁体   English

pickle和python数据结构

[英]pickle and python data structure

I have some data stored in a tree in memory and I regularly store the tree into disk using pickle. 我有一些数据存储在内存中的树中,我经常使用pickle将树存储到磁盘中。 Recently I noticed that the program using a large memory, then I checked saved pickle file, it is around 600M, then I wrote an other small test program loading the tree back into memory, and I found that it would take nearly 10 times memory(5G) than the size on disk, is that normal? 最近我注意到程序使用了大内存,然后检查了保存的pickle文件,大约600M,然后我编写了另一个小测试程序,将树重新加载到内存中,发现它会占用近10倍的内存( 5G)比磁盘上的大小还正常吗? And what's the best way to avoid that? 避免这种情况的最佳方法是什么?

No it's not normal. 不,这是不正常的。 I suspect your tree is bigger than you think. 我怀疑你的树比你想的要大。 Write some code to walk it and add up all the space used (and count the nodes). 编写一些代码来遍历它,并加总所有使用的空间(并计算节点数)。

See memory size of Python data structure 查看Python数据结构的内存大小

Also what exactly are you asking? 你到底在问什么? Are you surprised that a 600M data structure on disk is 5G in memory. 您是否对磁盘上的600M数据结构内存为5G感到惊讶? That's not particularly surprising. 这并不奇怪。 Pickle compresses the data so you expect it to be smaller on disk. Pickle会压缩数据,因此您希望它在磁盘上更小。 It's smaller by a factor of 10 (roughly) which is pretty good. 它要小10倍(大约),非常好。

If you're surprised by the size of your own data that's another thing. 如果您对自己的数据量感到惊讶,那是另一回事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM