dpzff20644 2011-10-22 18:14
浏览 54
已采纳

如何在PHP中捕获递归失控脚本?

My Apache gets killed sometimes because it runs out of memory. This did not use to happen. I have several hundred PHP files in this project.

I suspect that there was an unintentional recursion created -- not just a function calling itself, but function A calling B which then calls A again.

I've tried just reading the code and finding such recursion, but with no luck.

Is there a way I can tell PHP to keep track of ALL recursions, or throw a warning when its internal stack is over a certain size?

  • 写回答

1条回答 默认 最新

  • duanbushi1867 2011-10-22 18:48
    关注

    My Apache gets killed sometimes because it runs out of memory.

    Well that's at least a point to start with. I could read in your question you suspect that is caused by a PHP script.

    To find out which one, you need to look further. One way I could think of is that you enable PHP error logging. Then set the memory_limit to a low value so you can provoke errors where much memory consumption happens. You find the line where this happens in the error log (see as well Protocol of some PHP Memory Stretching Fun).

    This should give you some potential places.

    I'm not specifically sure if recursion must cause this and you wrote yourself that you only assume that. To detect recursion, you can use xdebug and limit recursion depth:

    xdebug.max_nesting_level=<your preferred value>
    

    This should give you as well more useful information in the error logs.

    If both memory and recursion is not the case, it might either not be related to PHP or PHP segaults and your webserver can't handle that situation. No idea how mod_php deals with segfaults for example but you haven't specified the SAPI you're using anyway.

    A typical source of PHP segfaults however are regular expressions that kick out PHP before triggering the pcre recursion limit. If you're making use of regular expressions in your code, you can just reduce the recursion limit to a lower value like and test if that helps. However, from your question it's not clear if that is a cause at all. So you first need to find out more.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题