2 回答
TA贡献1831条经验 获得超4个赞
设法在本地重现。
下面是一个示例应用程序,它在一个请求中分配一些整数,并在下一个请求中对它们进行垃圾收集:
// Package test implements a simple memory test for Google App Engine.
package test
import (
"net/http"
"runtime"
"appengine"
)
var buffer []int64
func init() {
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
var s runtime.MemStats
c := appengine.NewContext(r)
if len(buffer) == 0 {
// Allocate 2^22 integers.
runtime.ReadMemStats(&s)
c.Debugf("Memory usage: %d bytes (%d system).", s.Alloc, s.Sys)
buffer = make([]int64, 4*1024*1024)
for i, _ := range buffer {
buffer[i] = int64(i*i)
}
runtime.ReadMemStats(&s)
c.Debugf("Memory usage increased to: %d bytes (%d system).", s.Alloc, s.Sys)
} else {
// Remove all references to the slice pointed to by buffer.
// This should mark it for garbage collection.
runtime.ReadMemStats(&s)
c.Debugf("Memory usage: %d bytes (%d system).", s.Alloc, s.Sys)
buffer = nil
runtime.GC()
runtime.ReadMemStats(&s)
c.Debugf("After GC event: %d bytes (%d system).", s.Alloc, s.Sys)
}
w.WriteHeader(http.StatusTeapot)
}
使用开发服务器运行时:
$ ./go_appengine/dev_appserver.py test
2013/09/16 12:28:28 DEBUG: Memory usage: 833096 bytes (272681032 system).
2013/09/16 12:28:28 DEBUG: Memory usage increased to: 34335216 bytes (308332616 system).
INFO 2013-09-16 12:28:28,884 module.py:593] default: "GET / HTTP/1.1" 418 -
2013/09/16 12:28:29 DEBUG: Memory usage: 34345896 bytes (308332616 system).
2013/09/16 12:28:29 DEBUG: After GC event: 781504 bytes (308332616 system).
INFO 2013-09-16 12:28:29,560 module.py:593] default: "GET / HTTP/1.1" 418 -
2013/09/16 12:28:30 DEBUG: Memory usage: 791616 bytes (308332616 system).
2013/09/16 12:28:30 DEBUG: Memory usage increased to: 34337392 bytes (308332616 system).
INFO 2013-09-16 12:28:30,276 module.py:593] default: "GET / HTTP/1.1" 418 -
2013/09/16 12:28:36 DEBUG: Memory usage: 34347536 bytes (308332616 system).
2013/09/16 12:28:36 DEBUG: After GC event: 783632 bytes (308332616 system).
INFO 2013-09-16 12:28:36,224 module.py:593] default: "GET / HTTP/1.1" 418 -
看来内存分配和垃圾收集工作正常。但是,查看ps输出,似乎释放内存并没有减少进程的虚拟内存使用:
$ ps axo command,vsize,rss | ag go_app
/usr/bin/python2.7 ./go_app 381248 56608
$ ps axo command,vsize,rss | ag go_app
/usr/bin/python2.7 ./go_app 676324 57652
$ ps axo command,vsize,rss | ag go_app
/usr/bin/python2.7 ./go_app 750056 57856
$ ps axo command,vsize,rss | ag go_app
/usr/bin/python2.7 ./go_app 750056 57856
似乎运行底层 Go 实例的 Python 进程不断增加其虚拟内存,但它从未被释放。在生产服务器上似乎也发生了类似的事情:实例运行时报告的分配内存与内核报告的已使用内存不同。
- 2 回答
- 0 关注
- 264 浏览
添加回答
举报