我有一个将永远运行的 PHP 程序(不是网页套接字服务器)。在处理超过 1000 个请求后,程序最终因内存不足异常而崩溃。
这是一个link to my project .
这是一个link to my program .
我不确定为什么会发生这种情况,我尝试在程序中处理请求(onMessage)的函数中使用垃圾收集函数,但它不会导致任何变化。如有任何建议,我们将不胜感激。
最佳答案
投入大量精力,您或许可以暂时缓解这种情况。但最终您将无法运行非终止 PHP 应用程序。
查看 PHP is meant to die .本文讨论 PHP 的内存处理(除其他事项外),并特别关注为什么所有长时间运行的 PHP 进程最终都会失败。部分摘录:
There’s several issues that just make PHP the wrong tool for this. Remember, PHP will die, no matter how hard you try. First and foremost, there’s the issue of memory leaks. PHP never cared to free memory once it’s not used anymore, because everything will be freed at the end — by dying. In a continually-running process, that will slowly keep increasing the allocated memory (which is, in fact, wasted memory), until reaching PHP’s memory_limit value and killing your process without a warning. You did nothing wrong, except expecting the process to live forever. Under load, replace the “slowly” part for "pretty quickly".
There’s been improvements in the “don’t waste memory” front. Sadly, they’re not enough. As things get complex or the load increases, it’ll crash.
关于PHP 内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20908706/