在不同机器上分配大 ndarray 时,MemoryError 与“ValueError:数组太大”

Mat*_*s24 1 python memory arrays memory-management numpy

我在两个独立的设备\xe2\x80\x94MacBook Air 2013年中(笔记本电脑1)和ThinkPad X1 Yoga 3G(笔记本电脑2)\xe2\x80\x94上运行Python,并在两者上创建numpy数组。尽管两台笔记本电脑的内存相对相似:

\n\n

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

\n\n

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

\n\n

在观察MemoryError. 例如:

\n\n

笔记本电脑1

\n\n
>>> import numpy as np\n>>> np.zeros(int(5.*10.**12))\narray([0., 0., 0., ..., 0., 0., 0.])\n>>> np.zeros(int(6.*10.**12))\nPython(6138,0x7fffda9413c0) malloc: *** mach_vm_map(size=48000000000000) failed (error code=3)\n*** error: can\'t allocate region\n*** set a breakpoint in malloc_error_break to debug\nTraceback (most recent call last):\n  File "<stdin>", line 1, in <module>\nMemoryError\n
Run Code Online (Sandbox Code Playgroud)\n\n

笔记本电脑2

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

Linux:

\n\n
Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 19:16:44) \n[GCC 7.3.0] on linux\nType "help", "copyright", "credits" or "license" for more information.\n>>> import numpy as np\n>>> np.zeros(int(5.*10.**8))\narray([0., 0., 0., ..., 0., 0., 0.])\n>>> np.zeros(int(5.*10.**9))\nTraceback (most recent call last):\n  File "<stdin>", line 1, in <module>\nMemoryError\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

我显然错过了一些可能显而易见的东西。如果能澄清为什么会出现这种差异,我们将不胜感激。

\n

iva*_*eev 5

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

最有可能的是,您在第二台机器上使用 32 位 Python:在代码库中搜索错误消息numpy显示,在计算数组大小导致整数溢出时会引发此错误