# Can't save in background: fork: Cannot allocate memory

- **ID:** `database/redis-bgsave-fork-error`
- **Domain:** database
- **Category:** system_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

Redis 尝试执行 BGSAVE 或 BGREWRITEAOF 时，操作系统 fork() 系统调用失败，因为系统内存不足或 overcommit_memory 设置不当。

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Redis 6.2.12 | active | — | — |
| Redis 7.0.11 | active | — | — |
| Redis 7.2.1 | active | — | — |

## Workarounds

1. **调整系统 overcommit_memory 参数：echo 1 > /proc/sys/vm/overcommit_memory，或永久设置：在 /etc/sysctl.conf 中添加 vm.overcommit_memory = 1。** (90% success)
   ```
   调整系统 overcommit_memory 参数：echo 1 > /proc/sys/vm/overcommit_memory，或永久设置：在 /etc/sysctl.conf 中添加 vm.overcommit_memory = 1。
   ```
2. **减少 Redis 内存使用或增加系统交换空间，确保有足够内存供 fork() 使用。** (75% success)
   ```
   减少 Redis 内存使用或增加系统交换空间，确保有足够内存供 fork() 使用。
   ```

## Dead Ends

- **** — 增加 Redis 的 maxmemory 参数，但问题在于系统内存不足，而非 Redis 内存限制。 (70% fail)
- **** — 直接重启 Redis 服务，暂时释放内存但系统配置未改，问题会再次出现。 (80% fail)
