大数组上的Python MemoryError [英] Python MemoryError on large array
问题描述
这是我要运行的python脚本:
n = 50000000000 ##50 billion
b = [0]*n
for x in range(0,n):
b[x] = random.randint(1,899999)
...但是我得到的输出是:
E:\python\> python sort.py
Traceback (most recent call last):
File "E:\python\sort.py", line 8, in <module>
b = [0]*n
MemoryError
那么,我现在该怎么办?
正在生成的列表的大小(500亿而不是5).
int
对象实例占用24个字节(sys.getsizeof(int(899999))
,即随机数的上限),因此该列表将占用50,000,000,000 * 24 bytes
,大约为 1.09 TB .>
换句话说,要创建这样的列表,您的计算机中至少需要 1118 GB RAM .
我不知道您的用例是什么,但是您应该考虑尝试解决的另一种方法(也许定义 So, what do I do now? The size of the list you are generating (which is 50 billion not 5). An In other words to create such a list you would need at least 1118 GB of RAM in your computer. I don't know what your use case is, but you should consider a different approach to what you are trying to solve (maybe define a generator, or just don't store your numbers in memory and instead directly use the numbers in the for loop). 这篇关于大数组上的Python MemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!E:\python\> python sort.py
Traceback (most recent call last):
File "E:\python\sort.py", line 8, in <module>
b = [0]*n
MemoryError
int
object instance takes 24 bytes (sys.getsizeof(int(899999))
, the upper limit of your random numbers), so that list would take 50,000,000,000 * 24 bytes
, which is about 1.09 TB.