英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论