dug*_*ets 25 memory-management go memory-consumption
我是Go的新手,试图找出它如何管理内存消耗.
我的一个测试项目中的内存有问题.我不明白为什么当我的程序运行很长时间时,Go会使用越来越多的内存(从不释放它).
我正在运行下面提供的测试用例.在第一次分配后,程序使用近350 MB的内存(根据ActivityMonitor).然后我尝试释放它,ActivityMonitor显示内存消耗增加一倍.为什么?
我使用Go 1.0.3在OS X上运行此代码.
这段代码有什么问题?在Go程序中管理大变量的正确方法是什么?
在实现使用大量时间和内存的算法时,我遇到了另一个与内存管理相关的问题; 运行一段时间后,它会抛出"内存不足"异常.
package main
import ("fmt"
"time"
)
func main() {
fmt.Println("getting memory")
tmp := make([]uint32, 100000000)
for kk, _ := range tmp {
tmp[kk] = 0
}
time.Sleep(5 * time.Second)
fmt.Println("returning memory")
tmp = make([]uint32, 1)
tmp = nil
time.Sleep(5 * time.Second)
fmt.Println("getting memory")
tmp = make([]uint32, 100000000)
for kk, _ := range tmp {
tmp[kk] = 0
}
time.Sleep(5 * time.Second)
fmt.Println("returning memory")
tmp = make([]uint32, 1)
tmp = nil
time.Sleep(5 * time.Second)
return
}
Run Code Online (Sandbox Code Playgroud)
nem*_*emo 35
目前,go使用标记和清除垃圾收集器,它通常不会定义何时丢弃对象.
但是,如果你仔细观察,就会有一个go例程调用sysmon,只要你的程序执行,它就会运行并定期调用GC:
// forcegcperiod is the maximum time in nanoseconds between garbage
// collections. If we go this long without a garbage collection, one
// is forced to run.
//
// This is a variable for testing purposes. It normally doesn't change.
var forcegcperiod int64 = 2 * 60 * 1e9
(...)
// If a heap span goes unused for 5 minutes after a garbage collection,
// we hand it back to the operating system.
scavengelimit := int64(5 * 60 * 1e9)
Run Code Online (Sandbox Code Playgroud)
forcegcperiod确定强制调用GC的时间.scavengelimit确定何时将跨度返回到操作系统.Spans是一些可以容纳多个对象的内存页面.它们被保留了scavengelimit一段时间,并且如果没有任何物体被释放则被释放scavengelimit.
在代码中,您可以看到有一个跟踪选项.只要清道夫认为他需要清理,你就可以用它来看看:
$ GOGCTRACE=1 go run gc.go
gc1(1): 0+0+0 ms 0 -> 0 MB 423 -> 350 (424-74) objects 0 handoff
gc2(1): 0+0+0 ms 1 -> 0 MB 2664 -> 1437 (2880-1443) objects 0 handoff
gc3(1): 0+0+0 ms 1 -> 0 MB 4117 -> 2213 (5712-3499) objects 0 handoff
gc4(1): 0+0+0 ms 2 -> 1 MB 3128 -> 2257 (6761-4504) objects 0 handoff
gc5(1): 0+0+0 ms 2 -> 0 MB 8892 -> 2531 (13734-11203) objects 0 handoff
gc6(1): 0+0+0 ms 1 -> 1 MB 8715 -> 2689 (20173-17484) objects 0 handoff
gc7(1): 0+0+0 ms 2 -> 1 MB 5231 -> 2406 (22878-20472) objects 0 handoff
gc1(1): 0+0+0 ms 0 -> 0 MB 172 -> 137 (173-36) objects 0 handoff
getting memory
gc2(1): 0+0+0 ms 381 -> 381 MB 203 -> 202 (248-46) objects 0 handoff
returning memory
getting memory
returning memory
Run Code Online (Sandbox Code Playgroud)
如您所见,在获取和返回之间不会执行gc调用.但是,如果将延迟从5秒更改为3分钟(超过2分钟forcegcperiod),则gc将删除对象:
returning memory
scvg0: inuse: 1, idle: 1, sys: 3, released: 0, consumed: 3 (MB)
scvg0: inuse: 381, idle: 0, sys: 382, released: 0, consumed: 382 (MB)
scvg1: inuse: 1, idle: 1, sys: 3, released: 0, consumed: 3 (MB)
scvg1: inuse: 381, idle: 0, sys: 382, released: 0, consumed: 382 (MB)
gc9(1): 1+0+0 ms 1 -> 1 MB 4485 -> 2562 (26531-23969) objects 0 handoff
gc10(1): 1+0+0 ms 1 -> 1 MB 2563 -> 2561 (26532-23971) objects 0 handoff
scvg2: GC forced // forcegc (2 minutes) exceeded
scvg2: inuse: 1, idle: 1, sys: 3, released: 0, consumed: 3 (MB)
gc3(1): 0+0+0 ms 381 -> 381 MB 206 -> 206 (252-46) objects 0 handoff
scvg2: GC forced
scvg2: inuse: 381, idle: 0, sys: 382, released: 0, consumed: 382 (MB)
getting memory
Run Code Online (Sandbox Code Playgroud)
内存仍未释放,但GC将内存区域标记为未使用.当使用的跨度未使用且超过时,将开始释放limit.从清道夫代码:
if(s->unusedsince != 0 && (now - s->unusedsince) > limit) {
// ...
runtime·SysUnused((void*)(s->start << PageShift), s->npages << PageShift);
}
Run Code Online (Sandbox Code Playgroud)
这种行为当然可能随着时间的推移而改变,但我希望你现在能够感受到当物体被强行扔掉而不是被扔掉的时候.
正如zupa所指出的,释放对象可能无法将内存返回给操作系统,因此在某些系统上,您可能看不到内存使用情况的变化.根据golang-nuts上的这个帖子,这似乎是Plan 9和Windows的情况.
zzz*_*zzz 15
要最终(强制)收集未使用的内存,您必须调用runtime.GC().
variable = nil 可能会使事情无法到达并因此有资格收集,但它本身并不会释放任何东西.