# [KB]关于 TiDB 由于 SlowLog 导致 TiDB Server 占用大量内存

## Description

查看 TiDB -- Server -- Memory Usage 监控，TiDB Server 占用了大量内存，甚至导致 OOM

![know-issue-tidb-slowlog-memory-increase](/images/TiDB/know-issue-tidb-slowlog-memory-increase.png)

## Root Cause

大概率是 [https://github.com/pingcap/tidb/pull/21456](https://github.com/pingcap/tidb/pull/21456) 导致，当取消 Slow Query 查询时没有释放资源。

> **注意**
> 
> 当前没有能够判断的方法，需要提交研发帮忙确认

## Diagnostic Steps

1. 采集 Profile Debug 方法，IP 地址为内存占用多的 TiDB 服务器的 IP，端口为内存占用多的 TiDB_Status_Port 的端口

    `curl -G http://{TiDBIP}:10080/debug/zip?seconds=30" > profile.zip`

2. 解压 profile 文件，使用以下命令查看 heap 文件

    go tool pprof -http :<任意不使用端口> heap
    Serving web UI on <http://172.xx.xx.xx:8081>
    <http://172.xx.xx.xx:8081>

3. 访问生成的 Web UI，查看内存占用，可以看到大量内存消耗在 bytes.(\*Buffer).String，并且包含 slowQuery

    ![know-issue-tidb-slowquery-memory-increase1](/images/TiDB/know-issue-tidb-slowquery-memory-increase1.png)

4. 查看 goroutine 文件包含 slow_query.go 关键字

## Resolution

升级到 v4.0.9 及之后版本修复

PR：[https://github.com/pingcap/tidb/pull/21496](https://github.com/pingcap/tidb/pull/21496)

## Workaround

定期手工或者设置定时任务，清理 SlowLog 日志，降低内存占用。

## Affect Version(optional)

v4.0.0,v4.0.1,v4.0.2,v4.0.3,v4.0.4,v4.0.5,v4.0.6,v4.0.7,v4.0.8