You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Golang REST API+ECS内存过高无响应问题排查求助

Golang REST API内存泄漏排查求助

问题描述

部署在ECS上的Golang REST API出现内存泄漏问题,仅能通过重启修复。内存占用不会超过0.7,但达到该阈值时应用停止响应,必须重启。注:该问题并非由特定API导致。

已尝试操作

首次使用pprof分析内存分配空间,由于API返回加密JSON,怀疑加密方法是问题根源,希望确认该排查方向是否正确。

疑似代码及测试情况

业务逻辑代码片段:

dashboard := make(map[string]interface{})
dashboard["feature_banner"] = l_feature_data
dashboard["home_category"] = home_category_data
res["dashboard"] = dashboard

mres, _ := json.Marshal(res)
ress := ""
ress = util.EncryptDatas(string(mres))
c.JSON(200, gin.H{"code": 1, "result": ress})

加密方法代码片段:

func EncryptDatas(data string) string {
    bKey := []byte(key)
    bIV := []byte(iv)
    bPlaintext := PKCS5PaddingNew([]byte(data), aes.BlockSize, len(data))
    block, err := aes.NewCipher(bKey)
    if err != nil {
    }
    ciphertext := make([]byte, len(bPlaintext))
    mode := cipher.NewCBCEncrypter(block, bIV)
    mode.CryptBlocks(ciphertext, bPlaintext)
    return hex.EncodeToString(ciphertext)
}

pprof内存分配测试数据:

  • 保留EncryptDatas + String(res):分配2493.72MB,占总内存2679.79MB的93.06%
  • 移除EncryptDatas + String(res):分配1256.06MB,占总内存1436.63MB的87.43%
  • 仅保留String(res):分配1692.63MB,占总内存1847.71MB的91.61%

求助问题

  1. 目前的排查方向是否正确?此前从未使用过pprof。
  2. 加密逻辑是否是问题根源?hex.EncodeToString是否可能引发内存问题?
  3. 在Golang中能否手动清理堆内存以避免内存占用达到0.7阈值?

内容的提问来源于stack exchange,提问作者Vikas Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 06:32:50