# OutOfMemoryError：Java 堆空间（字段缓存驱逐）在加载字段 [user.name] 的字段数据时

- **ID:** `elasticsearch/field-cache-eviction-oom`
- **领域:** elasticsearch
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 74%

## 根因

用于文本字段聚合的字段数据缓存消耗过多堆内存，导致在缓存驱逐或加载期间发生 OutOfMemoryError。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Elasticsearch 7.10.0 | active | — | — |
| Elasticsearch 8.9.0 | active | — | — |
| Elasticsearch 8.14.0 | active | — | — |

## 解决方案

1. ```
   Enable `eager_fielddata` on the field mapping to preload fielddata at index time, reducing peak memory usage during queries. Update mapping: `PUT my_index/_mapping {"properties": {"user.name": {"type": "text", "fielddata": true, "eager_fielddata": true}}}`
   ```
2. ```
   Limit the fielddata cache size and set a circuit breaker. Configure in `elasticsearch.yml`: `indices.fielddata.cache.size: 20%` and `indices.breaker.fielddata.limit: 40%`. Then restart the node.
   ```

## 无效尝试

- **** — This only postpones the OOM error; large aggregations on high-cardinality text fields can still exhaust memory, and large heaps can cause longer GC pauses. (65% 失败率)
- **** — This can cause severe performance degradation because every aggregation will reload fielddata from disk, leading to high latency and potential OOM from concurrent loads. (75% 失败率)
