如何准确地跟踪内存使用情况?

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

How to track memory usage accurately?

问题

我正在尝试构建一个小工具,通过Go运行程序并跟踪内存使用情况。我使用r.exec = exec.Command(r.Command, r.CommandArgs...)来运行命令,并使用runtime.MemStats来跟踪内存使用情况(在单独的Go协程中):

func monitorRuntime() {
    m := &runtime.MemStats{}
    f, err := os.Create(fmt.Sprintf("mmem_%s.csv", getFileTimeStamp()))
    if err != nil {
        panic(err)
    }
    f.WriteString("Time;Allocated;Total Allocated; System Memory;Num Gc;Heap Allocated;Heap System;Heap Objects;Heap Released;\n")
    for {
        runtime.ReadMemStats(m)
        f.WriteString(fmt.Sprintf("%s;%d;%d;%d;%d;%d;%d;%d;%d;\n", getTimeStamp(), m.Alloc, m.TotalAlloc, m.Sys, m.NumGC, m.HeapAlloc, m.HeapSys, m.HeapObjects, m.HeapReleased))
        time.Sleep(5 * time.Second)
    }
}

当我用一个简单的程序进行测试(大约运行12小时),我注意到Go不断分配更多的内存:
System Memory
Heap Allocation

我进行了一些其他的测试,比如只运行monitorRuntime()函数而没有其他代码,或者使用pprof,例如:

package main

import (
    "net/http"
    _ "net/http/pprof"
)

func main() {
    http.ListenAndServe(":8080", nil)
}

然而,我仍然注意到内存分配量就像图表中一样不断增加。

我如何准确地跟踪我想要通过Go运行的程序的内存使用情况?
我知道一种方法,过去我使用过,就是使用/proc/$PID/statm,但是这个文件在每个操作系统上都不存在(比如MacOS或Windows)。

英文:

I am trying to build a small tool that will allow me to run a program and track memory usage through Go. I am using r.exec = exec.Command(r.Command, r.CommandArgs...) to run the command, and runtime.MemStats to track memory usage (in a separate go routine):

func monitorRuntime() {
	m := &runtime.MemStats{}
	f, err := os.Create(fmt.Sprintf("mmem_%s.csv", getFileTimeStamp()))
	if err != nil {
		panic(err)
	}
	f.WriteString("Time;Allocated;Total Allocated; System Memory;Num Gc;Heap Allocated;Heap System;Heap Objects;Heap Released;\n")
	for {
		runtime.ReadMemStats(m)
		f.WriteString(fmt.Sprintf("%s;%d;%d;%d;%d;%d;%d;%d;%d;\n", getTimeStamp(), m.Alloc, m.TotalAlloc, m.Sys, m.NumGC, m.HeapAlloc, m.HeapSys, m.HeapObjects, m.HeapReleased))
		time.Sleep(5 * time.Second)
	}
}

When I tested my code with simple program that just sits there (for about 12 hours), I noticed that Go is constantly allocating more memory:
System Memory
Heap Allocation

I did a few more tests such as running the monitorRuntime() function without any other code, or using pprof, such as:

package main

import (
	"net/http"
	_ "net/http/pprof"
)

func main() {
	http.ListenAndServe(":8080", nil)
}

Yet I still noticed that memory allocation keeps going up just like in the graphs.

How can I accurately track memory usage of the program I want to run through Go?
I know one way, which I used in the past, is to use /proc/$PID/statm, but that file doesn't exist in every operating system (such as MacOS or Windows)

答案1

得分: 1

在标准的Go语言中,没有一种方法可以获取通过exec.Command调用的程序的内存使用情况。runtime.ReadMemStats只返回Go运行时跟踪的内存(在这种情况下,仅包括文件处理和sprintf)。

你最好的选择是执行特定于平台的命令来获取内存使用情况。

英文:

There isn't a way in standard Go to get the memory usage of a program called from exec.Command. runtime.ReadMemStats only returns memory tracked by the go runtime (which, in this case, is only the file handling and sprintf).

Your best bet would be to execute platform specific commands to get memory usage.

答案2

得分: 0

在Linux(RedHat)上,以下命令将显示内存使用情况:
ps -No pid,comm,size,vsize,args:90

英文:

On Linux (RedHat) the following will show memory usage:
ps -No pid,comm,size,vsize,args:90

huangapple
  • 本文由 发表于 2017年1月31日 23:58:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/41961906.html
匿名

发表评论

匿名网友

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

确定