PHP 内存不足异常

标签 php memory

我有一个将永远运行的 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/

相关文章:

PHP PDO 动态查询构建

php - 在文本输入的值属性中使用 htmlspecialchars

memory - CUDA 纹理内存空间

go - ANTLR4内存使用情况

java - 如何使用 Apache POI 加载大型 xlsx 文件?

iphone - 我的 NSDateFormatter 脚本中存在重大泄漏

php - 在页面加载之前将 CSS img max-height + max-width 属性动态设置为窗口大小?

php - 使用 UPDATE MySqli PHP 增加 Int 值

php - 使用 Yammer Api 时返回无效的 OAuth 签名

c++ - 如果 std::vector 包含的元素尺寸增大,是否必须调整其大小?