# Elasticsearch异常：[脚本缓存] 驱逐触发，当前堆使用率[92%]，最大缓存大小[10000]

- **ID:** `elasticsearch/script-cache-eviction-memory-pressure`
- **领域:** elasticsearch
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

脚本缓存已满且堆内存使用率很高，导致Elasticsearch驱逐已编译的脚本以释放内存，从而降低性能。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.17.14 | active | — | — |
| 8.11.0 | active | — | — |
| 8.14.0 | active | — | — |

## 解决方案

1. ```
   在elasticsearch.yml中增加脚本缓存大小：script.cache.max_size: 20000并重启节点，或使用动态设置：PUT _cluster/settings { "transient": { "script.cache.max_size": "20000" } }
   ```
2. ```
   通过参数化内联脚本减少脚本种类：使用存储脚本或参数代替每次查询生成唯一脚本源，例如 { "script": { "id": "my_script", "params": { "factor": 2 } } }
   ```

## 无效尝试

- **** — Larger heap may delay eviction but does not fix the root cause of too many unique scripts being compiled; cache still fills up (60% 失败率)
- **** — Disabling the cache forces recompilation on every script execution, causing severe performance degradation and potential OOM (90% 失败率)
