georgehao / oblog

Apache License 2.0
0 stars 0 forks source link

posts/2023-04-19-go-runtime-keepalive/ #4

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Go runtime.KeepAlive | HHFCodeRv

在 Go 编程中,你是否注意过临时变量是什么时候被回收的?比如下面这段程序, 变量 a 会在什么时间被 GC 回收掉的? func f2() { // do something } func f1() int { var a int = 10 a += f2() b := a // do something return b } A. f1() 函数结束后的 GC 周期内 B. 当 a 复制给

https://www.haohongfan.com/posts/2023-04-19-go-runtime-keepalive/

cuishuang commented 1 year ago

范哥,压舱石这样定义也可以,不用runtime.KeepAlive。

var stub = make([]byte, 1010241024*1024)

func main() {

stub[0] = 1
fmt.Println("remain a not used bytes ", len(stub))

}