# ERR 运行脚本错误（调用 f_<sha>）：@user_script: <行>: -OOM 当已用内存超过 'maxmemory' 时不允许执行命令

- **ID:** `redis/lua-script-oom`
- **领域:** redis
- **类别:** resource_error
- **错误码:** `ERM`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

Lua 脚本尝试执行写命令，但 Redis 实例已超过 maxmemory 限制，触发了内存不足策略。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Redis 6.2.0 | active | — | — |
| Redis 7.0.0 | active | — | — |
| Redis 7.2.0 | active | — | — |

## 解决方案

1. ```
   Reduce script memory usage by batching operations: use redis.pcall with smaller key sets and avoid storing large intermediate results in Lua tables.
   ```
2. ```
   Increase maxmemory and enable an eviction policy: CONFIG SET maxmemory 2gb; CONFIG SET maxmemory-policy allkeys-lru. This allows Redis to free memory before the script runs.
   ```
3. ```
   Use MEMORY USAGE command to identify large keys and delete or archive them before rerunning the script.
   ```

## 无效尝试

- **** — SCRIPT KILL only stops the script, but the memory pressure remains; the script will fail again on next execution. (60% 失败率)
- **** — Blindly increasing maxmemory can lead to swap usage or OOM killer on the server; it does not fix the script's memory consumption. (70% 失败率)
- **** — Disabling eviction can cause Redis to crash with OOM if memory is exhausted; it is not a safe workaround. (85% 失败率)
