You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Goroutine栈限制从1GB提升至2GB?

How to Boost Goroutine Stack Size to 2GB in Go

Hey there! Let's tackle that stack overflow error you're running into. First, a quick context check: on 64-bit systems, Go's default maximum goroutine stack size is capped at 1GB—that's exactly why you're seeing the runtime: goroutine stack exceeds 1000000000-byte limit message.

Here's how to crank it up to 2GB

To increase this limit, you need to set a runtime variable at compile time using Go's -ldflags option. Here's the step-by-step:

  1. Convert 2GB to bytes: 2GB equals 2147483648 bytes (do the math: 2 * 1024 * 1024 * 1024).
  2. Compile your program with the custom flag:
    go build -ldflags="-X runtime.maxstacksize=2147483648" your-main-file.go
    
    Swap out your-main-file.go with the path to your actual main Go file or package name.
  3. Run the built binary: Once compiled with that flag, your program will allow goroutines to grow their stacks all the way to 2GB.

A few critical things to keep in mind

  • This is a global setting: Every goroutine in your program will have this 2GB maximum stack limit. If you're spawning multiple goroutines, each could potentially use up to 2GB of memory—make sure your system has enough virtual memory to handle that without hitting OS-level limits.
  • Check system memory constraints: Even though Go manages goroutine stacks in user space, some operating systems have total process memory limits. Double-check that your system can allocate the necessary virtual memory for your large stacks.
  • Double-check for hidden recursion: You mentioned there's no infinite recursion, but it's worth a quick second pass—deep nested logic can still chew through stack space, even with a larger limit. Better safe than sorry!

内容的提问来源于stack exchange,提问作者Tommy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:24:19