64GB 可用,cv2/python 超过 16GB 时崩溃

64GB available, cv2/python crash when exceeding 16GB

我正在将许多图像加载到内存中,因为我需要经常迭代它们以在训练神经网络时执行随机数据扩充。我的机器有64GB内存,还有60多GB可用。机器运行 64 位 Linux 和 Python 3.7.4.

我的脚本一直运行到进程超过 16GB。然后我看到这个错误信息:

cv2.error: OpenCV(3.4.2)/tmp/build/80754af9/opencv-suite_1535558553474/work/modules/core/src/alloc.cpp:55: error: (-4:Insufficient memory) Failed to allocate 18874368 bytes [this are 18MB] in function 'OutOfMemoryError'

cv2 是否有内存限制and/orpython?

我还用 numpy 尝试了以下操作:

a = np.zeros((16*1024*1024*1024,), dtype=np.uint8)+1

(工作并分配 16GB)

a = np.zeros((17*1024*1024*1024,), dtype=np.uint8)+1

(崩溃)

所以我认为这是一个 python 或 numpy 问题,因为 cv2 在内部使用 numpy。

有趣的是,我可以使用 pytorch 分配 >16GB:

a = torch.ones((28*1024*1024*1024,), dtype=torch.uint8)

(有效,但当我尝试超过 28GB 时失败)

忘了说我是 运行 SLURM 实例中的一切。但我不知道如何确定这是否是问题所在,因为我没有其他机器具有该内存量。

编辑:在加载每个图像之前,我使用 psutil 打印内存信息。这是它崩溃前的样子:

svmem(total=134773501952, available=116365168640, percent=13.7, used=17686675456, free=112370987008, active=18417344512, inactive=2524413952, buffers=176410624, cached=4539428864, shared=87986176, slab=371335168)

该问题与 Python and/or OpenCV 无关。我的 ulimit -v 设置太低了。 运行 ulimit -v unlimited 问题解决