【问题标题】:MemoryError vs "ValueError: array is too big" when allocating a large ndarray on different machines [duplicate]在不同机器上分配大型 ndarray 时,MemoryError 与“ValueError:数组太大”[重复]
【发布时间】:2019-07-31 01:06:03
【问题描述】:

我在两个不同的设备上运行 Python——MacBook Air mid-2013(笔记本电脑 1)和 ThinkPad X1 Yoga 3G(笔记本电脑 2)——并在这两个设备上创建 numpy 数组。尽管两台笔记本电脑的内存相对相似:

笔记本电脑 1:内存 4 GB 1600 MHz DDR3

笔记本电脑 2:安装 RAM 16.0 GB(可用 15.8 GB)

在观察MemoryError 之前,我发现两台笔记本电脑上的阈值数组大小截然不同。例如:

笔记本电脑 1

>>> import numpy as np
>>> np.zeros(int(5.*10.**12))
array([0., 0., 0., ..., 0., 0., 0.])
>>> np.zeros(int(6.*10.**12))
Python(6138,0x7fffda9413c0) malloc: *** mach_vm_map(size=48000000000000) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError

笔记本电脑 2

>>> import numpy as np
>>> np.zeros(int(1.*10.**8.))
array([0., 0., 0., ..., 0., 0., 0.])
>>> np.zeros(int(5.*10.**8.))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger than the maximum possible size.
>>> np.zeros(int(5.*10.**9.))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Maximum allowed dimension exceeded
>>> import numpy as np
>>> np.zeros(int(1.*10.**8.))
array([0., 0., 0., ..., 0., 0., 0.])
>>> np.zeros(int(5.*10.**8.))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger than the maximum possible size.
>>> np.zeros(int(5.*10.**9.))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Maximum allowed dimension exceeded

Linux:

Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 19:16:44) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.zeros(int(5.*10.**8))
array([0., 0., 0., ..., 0., 0., 0.])
>>> np.zeros(int(5.*10.**9))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError

因此,在收到MemoryError 之前,长度(大约)5 万亿的数组似乎是笔记本电脑 1 的极限,而笔记本电脑 2 的阈值接近 5 亿左右。这本质上是 4 个数量级的差异,但每台笔记本电脑上的可用内存几乎没有那么不同。我还尝试在两台具有大约 4 GB RAM 的 Linux 机器上创建类似的数组,并观察到大约 10 亿的长度是限制。

我显然遗漏了一些可能很明显的东西。任何澄清为什么观察到这种差异的情况将不胜感激。

【问题讨论】:

    标签: python arrays numpy memory memory-management


    【解决方案1】:

    由于您在第二台机器上得到的是 ValueError: array is too big; arr.size * arr.dtype.itemsize is larger than the maximum possible size. 而不是 MemoryError,原因一定是 Python 或 numpy 在该机器上的架构限制,而不是系统内存限制。

    很可能,您在第二台机器上使用的是 32 位 Pythonsearching the error message in numpy's codebase 表明 this error is raised when calculating array size results in integer overflow

    【讨论】:

    • 感谢您的意见。我提供了一个额外的示例,其中观察到了 MemoryError
    猜你喜欢
    • 1970-01-01
    • 2017-10-18
    • 2020-02-10
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2015-08-21
    • 2014-07-06
    • 1970-01-01
    相关资源
    最近更新 更多