0

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.

enter image description here

Number of go routins

Ysak
  • 2,601
  • 6
  • 29
  • 53
  • 2
    So after an hour of active use, your Go webserver allocated and used 30 MB of RAM, and you think that's much? There's nothing wrong in your graph. There's not enough data to safely state there is memory leak in your app. Try to profile for a longer time. Meanwhile check out this answer: [Golang - Cannot free memory once occupied by bytes.Buffer](https://stackoverflow.com/questions/37382600/golang-cannot-free-memory-once-occupied-by-bytes-buffer/37383604#37383604). – icza Aug 31 '17 at 09:08
  • 1
    I have checked it...and seen that it increased gradually after couple of days to 1 GB of memory. I am not worried about using memory, but unused memory it should clean up, be it 1MB, if its no more used by the application it should release. – Ysak Aug 31 '17 at 09:15
  • 2
    Also check out this answer for tips preventing allocation of big memory: [Freeing unused memory?](https://stackoverflow.com/questions/45509538/freeing-unused-memory/45509642#45509642) – icza Aug 31 '17 at 09:19
  • Have you tried profiling it with `pprof`? From this graph alone, there is nothing anyone here can do but guess, as we don't have your source code, we don't have profiler output, we don't have load details, all we know is "it's using memory". Maybe that's normal, maybe it isn't. If it isn't normal, there are many possible reasons - goroutine leaks, failure to close HTTP response bodies, unbounded slice expansion, reading large streams into memory... it could be *anything*. – Adrian Aug 31 '17 at 13:40
  • Correct, I am trying to do a profiling, first step i did is to use the stackimpact monitoring, and now I have to do a deep dive into the problem, My only worry is, a request is over once the http request is processed, even at any case, the life time of any data created through the request cycle is till the end of request. I am never seen the memory even went down at least usage hour, rather its always going up, So clearly, either i am doing something wrong.. i will use pprof and publish the details here, Posted this to see anyone faced same kind of issues in golang. – Ysak Sep 02 '17 at 06:38

1 Answers1

6

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?

peterSO
  • 158,998
  • 31
  • 281
  • 276