【发布时间】: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