[1700172.849656] Out of memory: Kill process 1195 (SliceDyson) score 730 or sacrifice child [1700172.849665] Killed process 1195 (SliceDyson) total-vm:13314968kB, anon-rss:8693980kB, file-rss:116kB
$ go tool pprof 120.pprof Type: inuse_space Time: Mar 6, 2019 at 9:27pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 603.20MB, 99.67% of 605.20MB total Dropped 1 node (cum <= 3.03MB) flat flat% sum% cum cum% 603.20MB 99.67% 99.67% 605.20MB 100% main.main 0 0% 99.67% 605.20MB 100% runtime.main (pprof) ^C
$ go tool pprof 240.pprof Type: inuse_space Time: Mar 6, 2019 at 11:42pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 2.81GB, 99.86% of 2.81GB total Dropped 6 nodes (cum <= 0.01GB) flat flat% sum% cum cum% 2.81GB 99.86% 99.86% 2.81GB 100% main.main 0 0% 99.86% 2.81GB 100% runtime.main
分析各个函数的占比情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
File: SliceDyson Type: inuse_objects Time: Mar 7, 2019 at 2:52pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) web (pprof) top Showing nodes accounting for 6810, 100% of 6810 total flat flat% sum% cum cum% 6554 96.24% 96.24% 6554 96.24% main.GetXMLFiles 256 3.76% 100% 256 3.76% vendor/golang_org/x/net/http2/hpack.addDecoderNode 0 0% 100% 256 3.76% main.init 0 0% 100% 6554 96.24% main.main 0 0% 100% 256 3.76% net/http.init 0 0% 100% 6810 100% runtime.main 0 0% 100% 256 3.76% vendor/golang_org/x/net/http2/hpack.init 0 0% 100% 256 3.76% vendor/golang_org/x/net/http2/hpack.init.0
最后定位到造成内存泄露的原因,是GetXMLFiles函数造成的。
1 2 3 4 5 6 7 8 9 10 11
var xmlFilesPath []string
var LoopGetXmlFilesPath []string var AllTasks []string var tasks []*Task for _, RootPath := range ParseStorageINI(StorageINIPath) { xmlFilesPath = GetXMLFiles(RootPath) for _, xmlFilesPath_str := range xmlFilesPath { LoopGetXmlFilesPath = append(LoopGetXmlFilesPath, xmlFilesPath_str) } }