# 错误：BGSAVE 失败，因为 fork 失败：无法分配内存

- **ID:** `redis/err-bgsave-failed-disk`
- **领域:** redis
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

Redis 服务器尝试 fork 子进程以执行 BGSAVE 失败，原因是系统内存不足，通常是由于内存过提交设置或高内存使用率。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Redis 6.2.8 | active | — | — |
| Redis 7.0.13 | active | — | — |
| Linux kernel 5.10 | active | — | — |
| Linux kernel 6.1 | active | — | — |

## 解决方案

1. ```
   Enable overcommit memory on the Linux host: `echo 1 > /proc/sys/vm/overcommit_memory` and make it persistent in /etc/sysctl.conf with `vm.overcommit_memory = 1`.
   ```
2. ```
   Reduce Redis memory usage by setting a lower `maxmemory` value or evicting keys with `maxmemory-policy allkeys-lru`. Restart Redis after changes.
   ```
3. ```
   Use a smaller fork-friendly configuration: set `repl-diskless-sync yes` to avoid fork during replication, or switch to AOF with `appendonly yes` and `no-appendfsync-on-rewrite yes`.
   ```

## 无效尝试

- **** — The fork failure is at the OS level, not Redis config. (95% 失败率)
- **** — Swap can provide a buffer; disabling it may cause immediate OOM kills. (70% 失败率)
