When will the stack of a goroutine shrink?

204 views
Skip to first unread message

tapi...@gmail.com

unread,
May 21, 2021, 10:45:54 AM5/21/21
to golang-nuts
From the outputs of the following program,
it looks the stack of a goroutine doesn't shrink immediately.
Will it shrink at some point eventually?


package main

func f(i int) byte {
    type T int // avoid f being inline
    var a [1<<20]byte // make stack grow
    return a[i]
}

func main(){
    var x int
    println(&x) // 0xc000034770
    f(100)
    println(&x) // 0xc0002fff70
   
    // It looks the stack hasn't shrunk here.
   
    f(100)
    println(&x) // 0xc0002fff70
}

Ian Lance Taylor

unread,
May 21, 2021, 4:16:45 PM5/21/21
to tapi...@gmail.com, golang-nuts
Stack shrinking occurs during garbage collection, and only if the
goroutine isn't doing anything and isn't sitting in a system call or a
cgo call.

Ian

tapi...@gmail.com

unread,
May 23, 2021, 4:21:08 AM5/23/21
to golang-nuts
Thanks for the explanation.
Reply all
Reply to author
Forward
0 new messages