[英]My golang program profiling using pprof shows memory increasing at json (* decodeState) objectInterface in std/json package
I have a golang program that uses unmarshall from std "encoding/json" package keeps increasing in size (memory leak).我有一个 golang 程序,它使用 std "encoding/json" 包中的 unmarshall 大小不断增加(内存泄漏)。 A diagram of memory profile using pprof shows memory increasing at json (* decodeState) objectInterface.
使用 pprof 的内存配置图显示了 json (* decodeState) objectInterface 的内存增加。 I want to understand how and why it could be happening to fix the issue.
我想了解解决问题的方式和原因。
I have tried several things at upper level like release of returned value to avoid the leak, but with no success.我在上层尝试了几件事,例如释放返回值以避免泄漏,但没有成功。
func (j JSONEncoding) From(b []byte, msg interface{}) (interface{}, error) {
err := json.Unmarshal(b, &msg)
return msg, err
}
pprof top5 shows this call, and the details below; pprof top5 显示了这个调用,以及下面的详细信息;
4096.89kB 24.43% 24.43% 4096.89kB 24.43% encoding/json.(*decodeState).objectInterface
(pprof) list objectInterface
Total: 20.65MB
ROUTINE ======================== encoding/json.(*decodeState).objectInterface in /usr/local/go/src/encoding/json/decode.go
6MB 7.50MB (flat, cum) 36.32% of Total
. . 1061: return v
. . 1062:}
. . 1063:
. . 1064:// objectInterface is like object but returns map[string]interface{}.
. . 1065:func (d *decodeState) objectInterface() map[string]interface{} {
3MB 3MB 1066: m := make(map[string]interface{})
. . 1067: for {
. . 1068: // Read opening " of string key or closing }.
.
. deleted code
.
. . 1095:
. . 1096: // Read value.
3MB 4.50MB 1097: m[key] = d.valueInterface()
using topN, and using visualization I can see this box increasing with time/processing.使用 topN,并使用可视化,我可以看到这个框随着时间/处理而增加。
This unmarshall is called in a loop but nothing is saved that could be a reason for the leak.这个解组在循环中被调用,但没有保存任何可能导致泄漏的原因。 I am not sure how and what of something to be done so that this leak is avoided.
我不确定要如何做以及要做什么来避免这种泄漏。
Update : The memory leak was more of a memory accrual, in the code at some other place.更新:在其他地方的代码中,内存泄漏更多是内存累积。 While trying to write a minimum code that shows the problem, I was not able to reproduce, and had to dig all the code to find out that an internal library was using a map to cache and the cache cleaner was not working properly.
在尝试编写显示问题的最小代码时,我无法重现,并且不得不挖掘所有代码以发现内部库正在使用映射进行缓存并且缓存清理器无法正常工作。
My problem was that pprof is giving info on who is allocating, but not where it is being kept.我的问题是 pprof 提供了关于谁正在分配的信息,而不是它被保存在哪里。 I think my question should have been, how can we find out which objects have references of what size.
我认为我的问题应该是,我们如何找出哪些对象具有多大尺寸的引用。 I know that one allocated reference could be in multiple places but such information would easily help in finding this kind of issue.
我知道一个分配的参考可能在多个地方,但这些信息很容易帮助找到这种问题。
Q: Why don't you do something like this:问:你为什么不做这样的事情:
https://golang.org/pkg/encoding/json/ https://golang.org/pkg/encoding/json/
func (a *Animal) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch strings.ToLower(s) {
default:
*a = Unknown
case "gopher":
*a = Gopher
case "zebra":
*a = Zebra
}
return nil
}
In other words, is your implementation preventing objects (like "err", for example) from being garbage collected?换句话说,您的实现是否阻止对象(例如“错误”)被垃圾收集?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.