# RuntimeWarning: 已打开超过20个图形。通过pyplot接口(matplotlib.pyplot)创建的图形会保留直到显式关闭，可能消耗过多内存。

- **ID:** `python/matplotlib-memory-leak-animation`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在循环中创建了大量图形但未关闭，导致内存累积。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |

## 解决方案

1. **Close figures after use with plt.close()** (95% 成功率)
   ```
   for i in range(100): plt.figure(); plt.plot([1,2,3]); plt.close()
   ```
2. **Use plt.close('all') at the end of the loop** (90% 成功率)
   ```
   for i in range(100): plt.figure(); plt.plot([1,2,3]); plt.close('all')
   ```

## 无效尝试

- **Increasing the warning threshold via rcParams** — This silences the warning but does not fix the memory leak. (90% 失败率)
- **Using plt.ioff() to disable interactive mode** — This only affects display, not memory management. (70% 失败率)
