# 运行时错误：DataLoader工作进程（pid 12345）收到信号11（段错误）。可能原因：共享内存耗尽、共享内存不足或工作进程过多。

- **ID:** `pytorch/data-loader-worker-segfault-shared-memory`
- **领域:** pytorch
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

DataLoader工作进程耗尽系统共享内存（/dev/shm）限制，在尝试将张量从共享内存复制到GPU时导致段错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| pytorch>=1.8.0 | active | — | — |
| linux>=4.18 | active | — | — |
| cuda>=11.0 | active | — | — |

## 解决方案

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.
   ```
2. ```
   Use `torch.utils.data.DataLoader(..., multiprocessing_context='spawn')` to force the spawn start method, which may reduce shared memory fragmentation on some systems.
   ```

## 无效尝试

- **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% 失败率)
- **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% 失败率)
