Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Do Gophers Dream of Stack Overflow?

Avatar for Takuto Nagami Takuto Nagami
May 22, 2026
9

Do Gophers Dream of Stack Overflow?

2026/5/22 Slides for the presentation at GopherCon Singapore.

Avatar for Takuto Nagami

Takuto Nagami

May 22, 2026

More Decks by Takuto Nagami

Transcript

  1. RAM from the application perspective . . . • Key-value

    store ◦ Key: Memory address ◦ Value: 1 byte for each addr • Most of high-level language (including Go) divides this store into two areas: Stack and Heap 0x0000 0x0001 0x0002 0x0003
  2. . . . main() stack frame 5 a() stack frame

    Stack memory area {v addr}
  3. . . . main() stack frame 5 a() stack frame

    5 Stack memory area {arg addr} {v addr}
  4. . . . main() stack frame 5 a() stack frame

    5 7 Stack memory area {arg addr} {a addr} {v addr}
  5. . . Stack memory area main() stack frame 5 a()

    stack frame {arg addr} {a addr} {v addr} 5 7 b() stack frame
  6. . . Stack memory area main() stack frame 5 a()

    stack frame {arg addr} {arg addr} {a addr} {v addr} 5 7 b() stack frame 7
  7. . . Stack memory area main() stack frame 5 a()

    stack frame {arg addr} {b addr} {arg addr} {a addr} {v addr} 5 7 b() stack frame 7 1
  8. . . . Stack memory area main() stack frame 5

    a() stack frame {arg addr} {a addr} {v addr} 5 7
  9. Stack is not a silver bullet... • Can’t handle unknown

    data size (maps/slices) ◦ Stack allocation is compiler’s duty • Sometimes can’t share variables between functions → These go to heap memory area
  10. Stack overflow • Language decides border between heap and stack

    ◦ e.g., in C on Linux, stack size is 8MB . . . . . Heap var main() stack frame
  11. Go has flexible stack • Stack for each goroutine •

    Each stack grows and shrinks on demand ◦ Initialized with 4kB
  12. Rough idea main goroutine main() stack frame a() stack frame

    a2() stack frame a3() stack frame a4() stack frame m2() stack frame b() stack frame a goroutine b goroutine
  13. Maximum stack size • Final size is defined when main

    goroutine starts ◦ runtime/proc.go ◦ 250MB for 32bit, 1GB for 64bit
  14. Let’s see an actual stack overflow! • Go says “fatal

    error: stack overflow” ◦ 6 million+ functions were nested
  15. Modifying the max size • The same experiment with 8

    MB of stack size ◦ Nested functions: 6 million -> 52,000