Golang:使用net/http时分配的堆内存不断增加(内存泄漏)。

huangapple go评论149阅读模式
英文:

Golang: Constant increase (Memory Leak) in allocated heap with net/http,

问题

我使用golang构建了一个应用程序API网关,使用golang反向代理。但是我发现内存逐渐增加,我尝试进行了性能分析,以下是在启动后几个小时内的图表。这是正常现象还是有什么问题?所有的分配都是来自go内置包和negroni mux。

Golang:使用net/http时分配的堆内存不断增加(内存泄漏)。

Golang:使用net/http时分配的堆内存不断增加(内存泄漏)。

英文:

I have used golang for building an application api gateway using golang reverse proxy, But i could able to see a gradual memory increase by time and i tried to profile, here is the graph in few hour time after starting. Is there anything wrong in this or is it expected. All the allocations are happening from go inbuilt packages and negroni mux.

Golang:使用net/http时分配的堆内存不断增加(内存泄漏)。

Golang:使用net/http时分配的堆内存不断增加(内存泄漏)。

答案1

得分: 6

当然,监控你的应用程序,以了解驱动使用情况的因素(当前请求的数量等),然后将其与资源使用情况(CPU、内存、当前存在的goroutine数量等)进行关联。你应该对应用程序资源使用情况建立因果模型,并监控显著偏差。总之,采取系统化的方法来测量和监控资源使用情况。

例如,监控并关联当前存在的goroutine数量。goroutine的数量应该围绕着基于关键应用程序驱动因素(如当前请求的数量)的稳定状态波动。

如果你的程序没有正确终止goroutine,它们就会变成无效的孤立goroutine。孤立的goroutine会保留内存,导致goroutine内存泄漏。你应该看到随着时间的推移内存使用量的稳定增加。

你的应用程序的goroutine模型是什么?你的应用程序的goroutine统计数据是什么?它们与内存是否相关?它们与时间的增加是否相关?

英文:

As a matter of course, monitor your applications so that you know what drives usage (number of current requests, etc), and then correlate that with resource usage (CPU, memory, number of goroutines that currently exist, etc). You should have a cause and effect model of your application resource usage and monitor for significant deviations. In summary, take a systematic approach to measuring and monitoring resource usage.

> Package runtime
>
> import "runtime"
>
> func NumGoroutine
>
> func NumGoroutine() int
>
> NumGoroutine returns the number of goroutines that currently exist.

For example, monitor and correlate the number of goroutines that currently exist. The number of goroutines should oscillate around a steady state based on key application drivers, like the number of current requests.

If you are not terminating goroutines properly in your program then they become inactive orphans. Orphan goroutines retain memory, a goroutine memory leak. You should see a steady increase in memory usage over time.

What is your goroutine model for your application? What are the goroutine statistics for your application? Do they correlate with memory. Do they correlate with increasing time?

huangapple
  • 本文由 发表于 2017年8月31日 16:49:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/45977181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定