Go: How Does the Goroutine Stack Size Evolve?
ℹ️ This article is based on Go 1.12.
Go provides a light and smart goroutines management. Light because the goroutine stack starts at 2Kb only, and smart since goroutines can grow / shrink automatically according to our needs.
Regarding the size of the stack, we can find it in runtime/stack.go
:
// The minimum size of stack used by Go code
_StackMin = 2048
We should note that it has evolved through the time:
- Go 1.2: goroutine stack has been increased from 4Kb to 8Kb.
- Go 1.4: goroutine stack has decreased from 8Kb to 2Kb.
The stack size has moved due to the strategy of the stack allocation. We will go back to this topic later in this article.
This default stack size is sometimes not enough to run our program. This is when Go automatically adjusts the size of the stack.
Dynamic stack size
If Go can automatically grow the size of the stack, it is also able to determine that the allocation size will not have to change. Let’s take an example and analyze how it works:
func main() {
a := 1
b := 2
r := max(a, b)
println(`max: `+strconv.Itoa(r))
}
func max(a int, b int) int {
if a >= b {…