我在代码中遇到了 numpy 错误 numpy.core._exceptions.MemoryError
。我的机器有足够的可用内存,所以这应该不是问题。
(这是在树莓派armv7l,4GB上)
$ free
total used free shared buff/cache available
Mem: 3748172 87636 3384520 8620 276016 3528836
Swap: 1048572 0 1048572
我找到了this post这表明我应该在内核中允许 overcommit_memory,所以我这样做了:
$ cat /proc/sys/vm/overcommit_memory
1
现在当我尝试运行这个示例时:
import numpy as np
arrays = [np.empty((18, 602, 640), dtype=np.float32) for i in range(200)]
我得到同样的错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
numpy.core._exceptions.MemoryError: Unable to allocate 26.5 MiB for an array with shape (18, 602, 640) and data type float32
为什么 python(或 numpy)会以这种方式运行,我怎样才能让它工作?
编辑: 回复中问题的答案:
这是32位系统(armv7l)
>>> sys.maxsize
2147483647
我打印了示例失败时的大致大小(根据错误消息,每次迭代应为 26.5MiB):
def allocate_arr(i):
print(i, i * 26.5)
return np.empty((18, 602, 640), dtype=np.float32)
arrays = [allocate_arr(i) for i in range(0, 200)]
输出显示,在分配的 RAM 约为 3GB 时,此操作会失败:
1 26.5
2 53.0
3 79.5
...
111 2941.5
112 2968.0
113 2994.5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
File "<stdin>", line 3, in allocate_arr
numpy.core._exceptions.MemoryError: Unable to allocate 26.5 MiB for an array with shape (18, 602, 640) and data type float32
3GB 是极限吗?有办法增加吗? 这难道不是过度 promise 的问题吗?
最佳答案
默认情况下,32 位 Linux 的用户/内核比例为 3:1。也就是说,在可以用 32 位无符号整数寻址的 4 GB 中,3 GB 保留给用户空间,1 GB 保留给内核空间。因此,任何单个进程最多可以使用 3 GB 内存。 vm.overcommit 设置与此无关,即使用比支持虚拟内存的实际物理内存更多的虚拟内存。
Linux 内核中曾经有所谓的 4G/4G 支持(不确定这些补丁是否曾经被纳入主流?),允许用户空间进程使用完整的 4 GB 空间,并允许用户空间进程使用另外 4 GB 地址空间内核,以性能较差为代价(TLB 在每个系统调用时刷新?)。但 AFAIU 这些功能已经有点烂了,因为每个对使用大量内存感兴趣的人很久以前就已经转向 64 位系统了。
关于linux - 即使 vm.overcommit_memory=1 ,numpy 也不会过度使用内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76356773/