【问题标题】:Spurious out-of-memory error when allocating shared memory with multiprocessing使用多处理分配共享内存时出现虚假的内存不足错误
【发布时间】:2019-11-26 20:00:30
【问题描述】:

我正在尝试使用multiprocessing.RawArray 在共享内存中分配一组图像缓冲区。它适用于较少数量的图像。但是,当我到达一定数量的缓冲区时,我得到一个OSError,表明我的内存已经用完了。

很明显的问题,我真的失忆了吗?据我计算,我尝试分配的缓冲区应该是大约 1 GB 的内存,并且根据 Windows 任务管理器,我有大约 20 GB 的可用空间。我不知道我怎么会真的失忆了!

我是否达到了某种可以增加的人为内存消耗限制?如果没有,为什么会发生这种情况,我该如何解决?

我使用的是 Windows 10,Python 3.7,64 位架构,总共 32 GB RAM。

这是一个可重现的最小示例:

import multiprocessing as mp
import ctypes

imageDataType = ctypes.c_uint8
imageDataSize = 1024*1280*3   # 3,932,160 bytes
maxBufferSize = 300
buffers = []
for k in range(maxBufferSize):
    print("Creating buffer #", k)
    buffers.append(mp.RawArray(imageDataType, imageDataSize))

输出:

Creating buffer # 0
Creating buffer # 1
Creating buffer # 2
Creating buffer # 3
Creating buffer # 4
Creating buffer # 5

...等等...

Creating buffer # 278
Creating buffer # 279
Creating buffer # 280
Traceback (most recent call last):
  File ".\Cruft\memoryErrorTest.py", line 10, in <module>
    buffers.append(mp.RawArray(imageDataType, imageDataSize))
  File "C:\Users\Brian Kardon\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\context.py", line 129, in RawArray
    return RawArray(typecode_or_type, size_or_initializer)
  File "C:\Users\Brian Kardon\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\sharedctypes.py", line 61, in RawArray
    obj = _new_value(type_)
  File "C:\Users\Brian Kardon\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\sharedctypes.py", line 41, in _new_value
    wrapper = heap.BufferWrapper(size)
  File "C:\Users\Brian Kardon\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\heap.py", line 263, in __init__
    block = BufferWrapper._heap.malloc(size)
  File "C:\Users\Brian Kardon\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\heap.py", line 242, in malloc
    (arena, start, stop) = self._malloc(size)
  File "C:\Users\Brian Kardon\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\heap.py", line 134, in _malloc
    arena = Arena(length)
  File "C:\Users\Brian Kardon\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\heap.py", line 38, in __init__
    buf = mmap.mmap(-1, size, tagname=name)
OSError: [WinError 8] Not enough memory resources are available to process this command

【问题讨论】:

    标签: python-3.x memory multiprocessing shared-memory allocation


    【解决方案1】:

    好的,the folks over at Python bug tracker figured this out for me。为后代:

    我使用的是 32 位 Python,它的内存地址空间限制为 4 GB,远小于我的可用系统总内存。显然,该空间中有足够的空间被其他东西占用,以至于解释器无法为我的所有RawArrays 找到足够大的连续块。

    使用 64 位 Python 时不会出现该错误,因此这似乎是最简单的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2012-05-15
      • 2015-11-09
      • 1970-01-01
      相关资源
      最近更新 更多