如何阻止php中的无限递归函数占用所有可用内存并最终使笔记本电脑崩溃? [英] How to stop an infinite recursive function in php from eating all the available memory and eventually crashing the laptop?
本文介绍了如何阻止php中的无限递归函数占用所有可用内存并最终使笔记本电脑崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这里有一个简单的无限递归代码:
I have a simple infinite recursive code here:
<?php
function test(){
test();
}
test();
当我尝试运行它时,它占用了所有内存,最终我的笔记本电脑挂了.我正在寻找的是一种方法来抓住这个并阻止机器挂起.php 中是否有相同的错误处理程序?
When I try to run the same, it eats up all the memory and eventually my laptop hangs. What I am looking for is a way to catch this and stop the machine from hanging. Is there any Error handler in php for the same?
我读过并尝试过的事情:将 max_execution_time 设置为 5秒并捕获错误行号.此项工作按建议进行.
Things I have read and tried: setting max_execution_time to lets say 5 secs and catch the error line no. This dint work as proposed.
还有什么其他方法可以捕捉和阻止这种情况?
Any other way to catch and stop this?
推荐答案
<?php
function test($tryCount=1){
$tryCount++;
//terminate recursive if more than 10 times loop
if($tryCount > 10){
return false;
}
test($tryCount);
}
test();
这篇关于如何阻止php中的无限递归函数占用所有可用内存并最终使笔记本电脑崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文