# OSError：无法对数据集文件进行内存映射：[Errno 12] 无法分配内存

- **ID:** `huggingface/dataset-memory-mapping-failure`
- **领域:** huggingface
- **类别:** system_error
- **错误码:** `ENOMEM`
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

系统没有足够的虚拟内存地址空间来对数据集文件进行内存映射，通常是由于文件过大或系统 ulimit 限制所致。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| datasets>=2.10.0 | active | — | — |
| Linux kernel >=5.4 | active | — | — |
| Python>=3.8 | active | — | — |

## 解决方案

1. ```
   通过将数据集文件分割成更小的分片来减小文件大小，然后再加载。使用 `datasets.load_dataset('path/to/dataset', split='train[:50%]')` 加载一半数据。
   ```
2. ```
   在Linux上使用 `sysctl -w vm.max_map_count=2000000` 增加系统的 vm.max_map_count。这增加了允许的内存映射区域数量。
   ```
3. ```
   使用 `load_dataset(..., streaming=True)` 通过流式传输数据块来避免对整个文件进行内存映射。
   ```

## 无效尝试

- **** — The error is about virtual memory address space, not physical RAM. Adding RAM does not fix address space exhaustion. (90% 失败率)
- **** — The error occurs during mmap of the original dataset file, not cache. Disabling caching may cause other issues and does not fix the mmap failure. (80% 失败率)
- **** — This error is about mmap, not mlock. The ulimit for locked memory is unrelated to virtual memory mapping limits. (95% 失败率)
