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.

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

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记