因为近期项目运行时,发现内存有一个规律性的增加。node
首先排查的是localcache的问题, 为了减小和redis的交互,对于一些热数据,同时更新频率也低,缓存周期内的数据延迟能够接受,采用了从redis读取到内存,进行二级缓存,缓存周期按数据量大小 为30s到10mins不等, 适当的调小内存中的周期减小了一部份内存的开销。golang
这种内存占用毕竟占少数。须要分析其余占用。web
用到的是 net/http/pprof 可在注册http路由时自定义,参考以下redis
path | api |
---|---|
/debug/pprof | pprof.Index |
/debug/pprof/cmdline | pprof.Cmdline |
/debug/pprof/profile | pprof.Profile |
/debug/pprof/symbol | pprof.Symbol |
/debug/pprof/trace | pprof.Trace |
在运行服务的机器上,安装分析工具(对pprof生成的文件进行分析)json
yum install graphvizapi
安装成功后,启动服务,执行下述命令缓存
go tool pprof ./mm-go localhost:1601/debug/pprof/heap函数
输入 top10 查看内存占用前10的函数工具
获得的响应以下优化
Fetching profile from http://localhost:8080/debug/pprof/heap Saved profile in /root/pprof/pprof.mm-go.localhost:1601.alloc_objects.alloc_space.inuse_objects.inuse_space.006.pb.gz Entering interactive mode (type "help" for commands) (pprof) top10 12369.14kB of 12369.14kB total ( 100%) Dropped 69 nodes (cum <= 61.84kB) Showing top 10 nodes out of 23 (cum >= 512.02kB) flat flat% sum% cum cum% 10320.21kB 83.44% 83.44% 10320.21kB 83.44% mm.com/priceServer.Worker.Start.func1.1 ...
其中详细的数据以下表格
这样就能看到不一样函数(func)占用内存大小以及占分配总内存的百分比了(flat)
flat | flat% | sum% | cum cum% | func |
---|---|---|---|---|
10320.21kB | 83.44% | 83.44% | 10320.21kB 83.44% | mm.com/priceServer.Worker.Start.func1.1 |
1024.41kB | 8.28% | 91.72% | 1024.41kB 8.28% | runtime.malg |
512.50kB | 4.14% | 95.86% | 512.50kB 4.14% | runtime.allocm |
512.02kB | 4.14% | 100% | 512.02kB 4.14% | runtime.rawstringtmp |
0 | 100% | 512.02kB 4.14% | encoding/json.(*decodeState).literal | |
0 | 100% | 512.02kB 4.14% | encoding/json.(*decodeState).literalStore | |
0 | 100% | 512.02kB 4.14% | encoding/json.(*decodeState).object | |
0 | 100% | 512.02kB 4.14% | encoding/json.(*decodeState).unmarshal | |
0 | 100% | 512.02kB 4.14% | encoding/json.(*decodeState).value | |
0 | 100% | 512.02kB 4.14% | encoding/json.Unmarshal |
根据提示查看内存占用较大的函数,来进行有针对性的优化