Python内存限制 [英] Python memory limit

查看:197
本文介绍了Python内存限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此很明显,Python中不会有无限的内存.我正在编写一个创建字典列表的脚本.但是每个列表有250K到6M个对象(有6个列表).

So clearly there cannot be unlimited memory in Python. I am writing a script that creates lists of dictionaries. But each list has between 250K and 6M objects (there are 6 lists).

是否可以实际计算(可能基于计算机的RAM)最大内存和运行脚本所需的内存?

在运行一个用1.25-150万个字典填充列表的脚本时,当它达到1.227时,它只是停止,但没有返回错误,更不用说MemoryError了.因此,我什至不确定这是否是内存限制.我有打印语句,因此我可以观察发生了什么,并且似乎一直缓冲着,因为什么都没有打印,直到该特定部分为止,代码每秒运行几千行. 有什么想法可以阻止它?这是记忆还是其他?

In running one of the scripts that populates a list with 1.25-1.5 million dictionaries, when it hits 1.227... it simply stops, but returns no error let alone MemoryError. So I am not even sure if this is a memory limit. I have print statements so I can watch what is going on, and it seems to buffer forever as nothing is printing and up until that specific section, the code is running a couple thousand lines per second. Any ideas as to what is making it stop? Is this memory or something else?

推荐答案

如果要存储的对象太多,则需要将它们存储在磁盘上,而不是内存中. 考虑使用数据库.

If you have that many objects you need to store, you need store them on disk, not in memory. Consider using a database.

如果导入sys模块,则可以访问函数 Python结构的内存中大小.

If you import the sys module you will have access to the function sys.getsizeof(). You will have to look at each object of the list and for each dictionary compute the value for every key. For more on this see this previous question - In-memory size of a Python structure.

这篇关于Python内存限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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