# [Known Issues] 持续删除大量数据再写入新数据可能导致 region cache 占用的内存持续上涨

## Key Words

内存持续/阶梯上涨，region cache

## Description

业务特征通常为大量删除数据之后再写入新数据，如果持续循环这一过程，可能导致 TiDB 内存随时间持续/阶梯上涨，疑似内存泄漏。

## Root Cause

[tikv/client-go#566](https://github.com/tikv/client-go/pull/566) 采用 lazy 的方式被动清理 region cache 过期信息，只在新 region 插入 region cache 时才会删除与之相交的旧 region 信息，即新的 region 需要覆盖过期的 region 范围，否则过期信息可能永远留在内存中，这样 region cache 过期信息堆积越来越多。

详见 [https://github.com/pingcap/tidb/issues/40461](https://github.com/pingcap/tidb/issues/40461)

## Diagnostic Steps

1. TiDB-Runtime 监控看到 Memory Usage 持续增加，Estimated Live Objects 也同步增长。

2. 对比不同时间的 TiDB Heap profile，RegionCache 部分的内存随着时间增加。

## Resolution

master 版本支持 region cache 主动清理机制，在后台持续扫描（每次扫描 50 个 region）并清理过期 region 信息(默认 TTL 10分钟)。
v6.6 发版后支持，其他 Release 版本 v5.4/v6.1/v6.5 需要持续跟踪修复版本。

请参考
[https://github.com/tikv/client-go/pull/664](https://github.com/tikv/client-go/pull/664)
[https://github.com/tikv/client-go/pull/678](https://github.com/tikv/client-go/pull/678)

## Workaround

重启内存占用高的 TiDB 实例

## Affected Versions

<= 6.5.0
