# RuntimeError: DataLoader worker (pid 12345) received signal 11 (Segmentation fault). Possible causes: shared memory exhaustion, insufficient shared memory, or too many workers.

- **ID:** `pytorch/data-loader-worker-segfault-shared-memory`
- **Domain:** pytorch
- **Category:** resource_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

DataLoader worker processes exhaust the system's shared memory (/dev/shm) limit, causing a segmentation fault when trying to copy tensors from shared memory to the GPU.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| pytorch>=1.8.0 | active | — | — |
| linux>=4.18 | active | — | — |
| cuda>=11.0 | active | — | — |

## Workarounds

1. **Increase the system shared memory limit by running the container or process with `--shm-size=8g` (Docker) or `mount -o remount,size=8G /dev/shm` (host). Alternatively, set `DataLoader(pin_memory=False, num_workers=0)` to bypass shared memory entirely.** (90% success)
   ```
   Increase the system shared memory limit by running the container or process with `--shm-size=8g` (Docker) or `mount -o remount,size=8G /dev/shm` (host). Alternatively, set `DataLoader(pin_memory=False, num_workers=0)` to bypass shared memory entirely.
   ```
2. **Use `torch.utils.data.DataLoader(..., multiprocessing_context='spawn')` to force the spawn start method, which may reduce shared memory fragmentation on some systems.** (75% success)
   ```
   Use `torch.utils.data.DataLoader(..., multiprocessing_context='spawn')` to force the spawn start method, which may reduce shared memory fragmentation on some systems.
   ```

## Dead Ends

- **Reduce the batch size to lower memory pressure.** — Reducing batch size does not directly affect shared memory usage per worker; each worker still allocates its own copy of tensors in shared memory for pin_memory. (70% fail)
- **Increase the number of DataLoader workers to speed up data loading.** — Increasing the number of workers exacerbates shared memory exhaustion by spawning more processes that each allocate their own shared memory buffers. (90% fail)
