As each goroutine is assigned a small stack, usually a few kB in size. The size of a stack is dynamic in go.
Resizing
Stack resizing the call stack occurs when a function needs more space.
- A larger stack is allocated
- All the contents of the stack are copied over to the larger stack
- The old stack is deallocated
Shrinking
Stack shrinking occurs when a garbage collection cycle notices large unused stack space. Shrinking of the stack space increases efficiency in memory management - increases locality of reference.
- Memory deallocated from the stack is released to the heap
Overhead
The process of shrinking and growing stack space incurs overhead - a new stack must be created, and all the contents of that stack need to copied each time a stack needs to grow. This is why stack shrinking only occurs when there is a significantly large stack space unused.
If a stack space is shrunk too early, but needs to grow again, this is an example of unnecessary overhead.