Golang:无法从内存溢出崩溃中恢复。

huangapple go评论85阅读模式
英文:

golang: cannot recover from Out Of Memory crash

问题

在某些情况下,调用append()会触发内存溢出的恐慌,并且似乎append()本身不会返回nil。

我该如何避免这种恐慌情况,并向用户显示“资源暂时不可用”?

最好的问候,

英文:

Under certain circumstances, calling append() triggers an out of memory panic and it seems append() itself doesn't return nil.

How could I avoid that panic scenario and show to my user "Resource temporary unavailable" ?

Best regards,

答案1

得分: 10

你不能。

如果运行时无法为append分配内存,可能无法恢复,或者向用户通信“资源暂时不可用”。例如,垃圾回收可能需要分配内存进行清理,或者调度程序可能正在尝试分配新线程。由于无法严格控制Go程序中的分配,因此无法优雅地处理内存耗尽的情况。

所有的内存耗尽条件都会终止Go程序的运行。

英文:

You can't.

If the runtime can't allocate memory for append, it may not be able to recover, or communicate "Resource temporary unavailable" to the user. For example, GC might need to allocate to clean up, or the scheduler might be trying to allocate a new thread. Because there's no way to strictly control allocations in a Go program, there's no way to gracefully handle running out of memory.

All OOM conditions terminate a Go program.

huangapple
  • 本文由 发表于 2015年6月1日 23:25:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/30577308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定