Search code examples
pythonlinuxshared-memorycoredump

Allocating large shared memory leads to "Bus error (core dumped)"


When allocating a large amount of shared memory (60% of total 128GiB RAM, 3GiB used), I got "Bus error (core dumped)" after several seconds and the memory usage went up. But if I allocate normal memory (in b()), it works ok.

from multiprocessing.managers import SharedMemoryManager
import numpy as np

SIZE = 70462337280  # Exact size is not quite important

def spin():
    import time
    while True:
        time.sleep(60)

def a():
    with SharedMemoryManager() as smm:
        shm = smm.SharedMemory(size=SIZE)
        buf = np.ndarray((SIZE,), dtype=np.uint8, buffer=shm.buf)
        buf.fill(0)
        print("Done")
        spin()

def b():
    buf = np.empty((SIZE,), dtype=np.uint8)
    buf.fill(0)
    print("Done")
    spin()

# Bus error (core dumped)
a()

# Works fine
# b()

I checked /proc/sys/kernel/shmmax, but that doesn't seem to be bounded.

cat /proc/sys/kernel/shmmax
18446744073692774399

Solution

  • It turns out that /dev/shm is a tmpfs which has a size limit of 50% of total RAM by default (can be checked with findmnt -o AVAIL,USED /dev/shm). When the space is used up, a BUS ERROR is raised.

    This can be resolved by modifying mount options. Check Shared Memory section in https://help.ubuntu.com/community/StricterDefaults for details.