# RuntimeError: DataLoader 工作进程（pid 12345）收到信号 11（段错误）。可能原因：共享内存耗尽或共享内存文件损坏。

- **ID:** `pytorch/dataloader-shared-memory-segfault`
- **领域:** pytorch
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

DataLoader 工作进程使用多进程和共享内存（shm）时，/dev/shm 空间耗尽或共享内存文件损坏，导致段错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| torch>=1.10 | active | — | — |
| torch>=2.0 | active | — | — |
| Linux kernel 5.x+ | active | — | — |
| Docker containers | active | — | — |

## 解决方案

1. ```
   Increase /dev/shm size: docker run --shm-size=8g ... or sudo mount -o remount,size=8G /dev/shm
   ```
2. ```
   Set DataLoader with multiprocessing_context='spawn' and reduce num_workers: DataLoader(dataset, num_workers=2, multiprocessing_context='spawn')
   ```
3. ```
   Disable shared memory by setting DataLoader with persistent_workers=False and pin_memory=False
   ```

## 无效尝试

- **Increasing num_workers to speed up loading** — More workers consume more shared memory, exacerbating the exhaustion and making segfaults more frequent. (70% 失败率)
- **Setting multiprocessing_context to 'fork' on Linux** — Fork inherits parent's memory space, but shared memory issues remain; 'spawn' is recommended for PyTorch. (60% 失败率)
