github.com/godaddy-x/freego@v1.0.156/gc/ballast.go (about) 1 package ballast 2 3 import ( 4 "fmt" 5 "math" 6 "runtime" 7 "runtime/debug" 8 "time" 9 ) 10 11 const ( 12 KB = 1024 13 MB = 1024 * KB 14 GB = 1024 * MB 15 ) 16 17 // GC 1.触发条件间隔2分钟 2.达到内存堆阀值 3.手动触发runtime.GC 4.启动GC触发 18 // 触发阀值 limit * percent% * 2 (percent default 100%) 19 func GC(limit int, percent int) { 20 if percent > 0 { 21 debug.SetGCPercent(percent) 22 } 23 if limit <= 0 { 24 limit = 128 * MB 25 } 26 fmt.Println(fmt.Sprintf("GC setting limit:%dMB, percent:%d%s, trigger:%dMB", limit/MB, percent, "%", limit/MB*percent/100*2)) 27 go func() { 28 ballast := make([]byte, limit) 29 <-time.After(time.Duration(math.MaxInt64)) 30 runtime.KeepAlive(ballast) 31 }() 32 }