golang 运行时:无法创建新的 OS 线程(已经有 2049;errno=12)
golang runtime: failed to create new OS thread (have 2049 already; errno=12)
我在 MacOs 上创建了很多 goroutine,程序执行时出现错误。
goRoutineId = 3710, i = 3683, len(chan) = 2049
runtime: failed to create new OS thread (have 2049 already; errno=12)
fatal error: runtime.newosproc
所以我想知道 "failed to create new OS thread" 是什么意思,这是操作系统的限制,只是 golang 没有能力创建更多的 goroutine?
谢谢你帮助我。
这是OS的限制。我假设您使用的是 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), when the errno=12
, the error reason is 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.
我在 MacOs 上创建了很多 goroutine,程序执行时出现错误。
goRoutineId = 3710, i = 3683, len(chan) = 2049
runtime: failed to create new OS thread (have 2049 already; errno=12)
fatal error: runtime.newosproc
所以我想知道 "failed to create new OS thread" 是什么意思,这是操作系统的限制,只是 golang 没有能力创建更多的 goroutine? 谢谢你帮助我。
这是OS的限制。我假设您使用的是 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), when the errno=12
, the error reason is 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.