【问题标题】:python multiprocessing.Array: huge temporary memory overheadpython multiprocessing.Array:巨大的临时内存开销
【发布时间】:2016-12-12 09:44:51
【问题描述】:

如果我使用 python 的 multiprocessing.Array 创建一个 1G 的共享数组,我发现 python 进程在调用 multiprocessing.Array 期间使用了大约 30G 的内存,然后减少了内存使用量。我将不胜感激任何帮助找出为什么会发生这种情况并解决它。

这是在 Linux 上重现它的代码,内存由 smem 监控:

import multiprocessing
import ctypes
import numpy
import time
import subprocess
import sys

def get_smem(secs,by):
    for t in range(secs):
        print subprocess.check_output("smem")
        sys.stdout.flush()
        time.sleep(by)



def allocate_shared_array(n):
    data=multiprocessing.Array(ctypes.c_ubyte,range(n))
    print "finished allocating"
    sys.stdout.flush()


n=10**9
secs=30
by=5
p1=multiprocessing.Process(target=get_smem,args=(secs,by))
p2=multiprocessing.Process(target=allocate_shared_array,args=(n,))
p1.start()
p2.start()
print "pid of allocation process is",p2.pid
p1.join()
p2.join()
p1.terminate()
p2.terminate()

这是输出:

pid of allocation process is 2285
  PID User     Command                         Swap      USS      PSS      RSS
 2116 ubuntu   top                                0      700      773     1044
 1442 ubuntu   -bash                              0     2020     2020     2024
 1751 ubuntu   -bash                              0     2492     2528     2700
 2284 ubuntu   python test.py                     0     1080     4566    11924
 2286 ubuntu   /usr/bin/python /usr/bin/sm        0     4688     5573     7152
 2276 ubuntu   python test.py                     0     4000     8163    16304
 2285 ubuntu   python test.py                     0   137948   141431   148700

  PID User     Command                         Swap      USS      PSS      RSS
 2116 ubuntu   top                                0      700      773     1044
 1442 ubuntu   -bash                              0     2020     2020     2024
 1751 ubuntu   -bash                              0     2492     2528     2700
 2284 ubuntu   python test.py                     0     1188     4682    12052
 2287 ubuntu   /usr/bin/python /usr/bin/sm        0     4696     5560     7160
 2276 ubuntu   python test.py                     0     4016     8174    16304
 2285 ubuntu   python test.py                     0 13260064 13263536 13270752

  PID User     Command                         Swap      USS      PSS      RSS
 2116 ubuntu   top                                0      700      773     1044
 1442 ubuntu   -bash                              0     2020     2020     2024
 1751 ubuntu   -bash                              0     2492     2528     2700
 2284 ubuntu   python test.py                     0     1188     4682    12052
 2288 ubuntu   /usr/bin/python /usr/bin/sm        0     4692     5556     7156
 2276 ubuntu   python test.py                     0     4016     8174    16304
 2285 ubuntu   python test.py                     0 21692488 21695960 21703176

  PID User     Command                         Swap      USS      PSS      RSS
 2116 ubuntu   top                                0      700      773     1044
 1442 ubuntu   -bash                              0     2020     2020     2024
 1751 ubuntu   -bash                              0     2492     2528     2700
 2284 ubuntu   python test.py                     0     1188     4682    12052
 2289 ubuntu   /usr/bin/python /usr/bin/sm        0     4696     5560     7160
 2276 ubuntu   python test.py                     0     4016     8174    16304
 2285 ubuntu   python test.py                     0 30115144 30118616 30125832

  PID User     Command                         Swap      USS      PSS      RSS
 2116 ubuntu   top                                0      700      771     1044
 1442 ubuntu   -bash                              0     2020     2020     2024
 1751 ubuntu   -bash                              0     2492     2527     2700
 2284 ubuntu   python test.py                     0     1192     4808    12052
 2290 ubuntu   /usr/bin/python /usr/bin/sm        0     4700     5481     7164
 2276 ubuntu   python test.py                     0     4092     8267    16304
 2285 ubuntu   python test.py                     0 31823696 31827043 31834136

  PID User     Command                         Swap      USS      PSS      RSS
 2116 ubuntu   top                                0      700      771     1044
 1442 ubuntu   -bash                              0     2020     2020     2024
 1751 ubuntu   -bash                              0     2492     2527     2700
 2284 ubuntu   python test.py                     0     1192     4808    12052
 2291 ubuntu   /usr/bin/python /usr/bin/sm        0     4700     5481     7164
 2276 ubuntu   python test.py                     0     4092     8267    16304
 2285 ubuntu   python test.py                     0 31823696 31827043 31834136

Process Process-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "test.py", line 17, in allocate_shared_array
    data=multiprocessing.Array(ctypes.c_ubyte,range(n))
  File "/usr/lib/python2.7/multiprocessing/__init__.py", line 260, in Array
    return Array(typecode_or_type, size_or_initializer, **kwds)
  File "/usr/lib/python2.7/multiprocessing/sharedctypes.py", line 115, in Array
    obj = RawArray(typecode_or_type, size_or_initializer)
  File "/usr/lib/python2.7/multiprocessing/sharedctypes.py", line 88, in RawArray
    result = _new_value(type_)
  File "/usr/lib/python2.7/multiprocessing/sharedctypes.py", line 63, in _new_value
    wrapper = heap.BufferWrapper(size)
  File "/usr/lib/python2.7/multiprocessing/heap.py", line 243, in __init__
    block = BufferWrapper._heap.malloc(size)
  File "/usr/lib/python2.7/multiprocessing/heap.py", line 223, in malloc
    (arena, start, stop) = self._malloc(size)
  File "/usr/lib/python2.7/multiprocessing/heap.py", line 120, in _malloc
    arena = Arena(length)
  File "/usr/lib/python2.7/multiprocessing/heap.py", line 82, in __init__
    self.buffer = mmap.mmap(-1, size)
error: [Errno 12] Cannot allocate memory

【问题讨论】:

  • 如果您使用的是 python 2,请将 range(n) 替换为 xrange(n)

标签: python arrays memory multiprocessing overhead


【解决方案1】:

从打印语句的格式来看,您使用的是 python 2

range(n) 替换为xrange(n) 以节省一些内存。

data=multiprocessing.Array(ctypes.c_ubyte,xrange(n))

(或使用 python 3)

10 亿范围大约需要 8GB(好吧,我刚刚在我的 Windows PC 上尝试过,但它冻结了:别那样做!)

尝试使用 10**7 来确定:

>>> z=range(int(10**7))
>>> sys.getsizeof(z)
80000064  => 80 Megs! you do the math for 10**9

xrange 这样的生成器函数不占用内存,因为它在迭代时会一一提供值。

在 Python 3 中,他们一定已经厌倦了这些问题,发现大多数人使用 range 是因为他们想要生成器,杀死了 xrange 并将 range 变成了生成器。现在,如果您真的想将所有必须分配的号码分配给list(range(n))。至少你不会误分配 1TB!

编辑:

OP 评论表示我的解释没有解决问题。我在我的 windows box 上做了一些简单的测试:

import multiprocessing,sys,ctypes
n=10**7

a=multiprocessing.RawArray(ctypes.c_ubyte,range(n))  # or xrange
z=input("hello")

使用 python 2 上升到 500Mb,然后保持在 250Mb 上升到 500Mb,然后使用 python 3 保持在 7Mb(这很奇怪,因为它至少应该是 10Mb...)

结论:好的,它的峰值为 500Mb,因此不确定它是否会有所帮助,但是您可以在 Python 3 上尝试您的程序,看看您的总体内存峰值是否更少?

【讨论】:

  • 不幸的是,问题不在于范围,因为我只是把它作为一个简单的说明。实际上,该数据将从磁盘读取。我还可以使用 n*["a"] 并在 multiprocessing.Array 中指定 c_char 作为另一个示例。当我传递给 multiprocessing.Array 的列表中只有 1G 数据时,它仍然使用大约 16G。我想知道是否有一些低效的酸洗或类似的事情。
【解决方案2】:

不幸的是,问题不在于范围,因为我只是把它作为一个简单的说明。实际上,该数据将从磁盘读取。我还可以使用 n*["a"] 并在 multiprocessing.Array 中指定 c_char 作为另一个示例。当我传递给 multiprocessing.Array 的列表中只有 1G 数据时,它仍然使用大约 16G。我想知道是否正在进行一些低效的酸洗或类似的事情。

我似乎通过使用 tempfile.SpooledTemporaryFile 和 numpy.memmap 找到了我需要的解决方法。我可以打开内存映射到内存中的临时文件,必要时将其假脱机到磁盘,并通过将其作为参数传递给 multiprocessing.Process 在不同进程之间共享它。

我仍然想知道 multiprocessing.Array 发生了什么。我不知道为什么它会为一个 1G 的数据数组使用 16G。

【讨论】:

  • 好的,range 到此为止。你能发布一个可复制的独立示例吗? (嗯,这里称为 MVCE)
猜你喜欢
  • 2017-12-20
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多