golang运行时:无法创建新的OS线程(已经有2049; errno = 12)

时间:2017-09-29 08:18:07

标签: go goroutine

我在MacO上创建了很多goroutine,程序执行时发出错误。

goRoutineId = 3710, i = 3683, len(chan) = 2049
runtime: failed to create new OS thread (have 2049 already; errno=12)
fatal error: runtime.newosproc

所以我想知道"未能创建新的OS线程"意思是,操作系统的限制,只是golang没有能力创造更多的goroutine? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

操作系统的限制。我假设你使用的是linux。

根据the source of go, 它正在调用clone系统调用

ret := clone(cloneFlags, stk, unsafe.Pointer(mp), unsafe.Pointer(mp.g0), unsafe.Pointer(funcPC(mstart)))
sigprocmask(_SIG_SETMASK, &oset, nil)

if ret < 0 {
    print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", -ret, ")\n")
    if ret == -_EAGAIN {
        println("runtime: may need to increase max user processes (ulimit -u)")
    }
    throw("newosproc")
}
来自manpage of clone(2)

,当errno=12时,错误原因为out of memory

ENOMEM Cannot allocate sufficient memory to allocate a task structure
              for the child, or to copy those parts of the caller's context
              that need to be copied.