在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
echo list | go tool pprof -alloc_space gateway http://10.2.1.93:8421/debug/pprof/heap > abc.log
echo list | go tool pprof -inuse_space gateway http://10.2.1.93:8421/debug/pprof/heap > inuse.log
https://deferpanic.com/blog/understanding-golang-memory-usage/
Mon, Dec 22, 2014 Warning: This is an intro to memory with the go language - you can deep dive down the rabbit hole as far as you want to go. Most beginning go developers try out a simple hello world ala:
and then they go completely crazy. 138 f!*%!G of $%@# memory!? This laptop only has 16G! Virtual vs Resident Go manages memory differently than what you might be used to. It will reserve a large chunk right off the bat (VIRT) but your (RSS) is much closer to reality of what is in use. What is the difference between RSS and VIRT ? VIRT or the virtual address space size is the amount that a program has mapped in and is able to access. RSS or the resident set size is the amount of memory actually in use. If you are curious about how go actually goes about doing this check out: https://github.com/golang/go/blob/master/src/runtime/malloc1.go
It’s important to note that if you are using 32bit arch the memory reservation is done completely differently. Garbage Collection Now that we know the difference between resident and shared memory we can talk about how go does garbage collection to understand how our program is working. Chances are you are writing some long lived daemon - be it a web app server or something more complex. Generally you will probably make quite a few allocations throughout it’s lifetime. Knowing how the memory is dealt with is essential. Typically if you go 2 minutes without garbage collection it will get ran. If a span goes unused for 5 minutes the scavenger allows it to be released. So if you suspect that your memory usage should be going back down give it ~ 7 minutes just to verify. Be aware that currently the gc is non-compacting - what this really means is that if you have a single byte touching a page - the scavenger will be prevented from madvising it. Last but not least - on go 1.3 goroutine stacks, which are 8k/pop, don’t get released - they get re-used later on. Don’t fret though - Go still has plenty of room for improvement in the GC department. So if your code is spawning a ton of goroutines and your RES is staying high this could be why. So, now we know what to look at it from outside our program and we know what to expect from GC. Analyzing Memory Usage. Let’s take a small example to how we might look at our memory. In our example we’ll allocate 10 sets of 100 megabytes. Then we’ll include a couple different ways of looking at the memory usage. One method is by using the runtime package and looking at the ReadMemStats function. The other method is using this super sweet web interface via the pprof package. This allows us to remotely grab our pprof data which we’ll explore shortly. Yet another method which Dave Cheney mentioned we should mention is to use the gctrace debug environment variable. Note: This was done on 64bit linux with go 1.4.
There are typically two options you might choose when using pprof to look at memory. One option is ‘–alloc_space’ which tells you how many megabytes have been allocated. The other – ‘–inuse_space’ tells you know how many are still in use. We can launch pprof and point it at our in-app webserver to get the topk abusers. Then if we want we can use list to see where some of that usage is coming from: In Use
Allocated
Topk is nice but what is nicer is the list command where we can see where the actual damage is being done in context to the rest of the program.
Those of you following at home have probably noticed quite a few differences in the memory usage being reported – why is that? Let’s look at ps =>
Now let’s look at our log output =>
Finally - let’s look at at using gctrace:
This is important because most ops tools will be looking at your application from the operating system’s point of view - not necessarily what is truly going on. More options can be found in the runtime package Short Answer:
Further - it is important to note that with pprof you are only getting a sampling - not the true values. In general - when looking at this sort of stuff it’s best to not focus on the numbers but focus on the problem. We at deferpanic believe in measuring everything but we feel “modern day” ops tools are horrible and focus on the effect of a problem but not the actual problem. If your car won’t start you may think that it is the problem but it’s not. It’s not even the fact that the gastank is empty. The real problem is that you did not put gas into the gastank and now you are noticing a stream of consequences from the original problem. If you were just monitoring the RES output from ps for a go binary - it might tell you that there’s a problem but you have no clue what the problem is until you start deep diving. We want to fix that. Edit: The next paragraph is left un-edited. It was not written to degrade ops or devops people. The intent was to show the difference between app level metrics and os level metrics. We realize this was not written well and apologize. We feel that existing ‘ops’ tools don’t give the developer the full information needed to fix their problems. We also feel that existing app level metric tools leave a lot to desire. Ops people play a very vital role and we are extremely thankful for all their work - indeed it’s the developers code that is messing things up - this is what we are looking at. End Edit Let the ops people have their 300+ graphs with a bajillion gauges and counters and meters and histograms. As people who actually write software we are more interested in finding the real solution by finding the real problem. |
请发表评论