有时候我们跑脚本时候。因为某些原因 内存溢出 导致 脚本停止,但是我们日志又捕捉不到信息,就很蛋疼
完整测试代码
class Test
{
public $name = "sdf";
public function __construct()
{
}
}
//写法1,这里内存不会溢出
while(true) {
$i++;
var_dump($i);
$i = new Test();
}
//写法2,这里内存不会溢出
while(true) {
new Test();
}
//写法3,这里内存会溢出
while(true) {
$i[] = new Test();
}
我们按照这个示例代码来测试
test.php 文件
class Test{
public $name = "laofan";
public function __construct()
{
}
}
try {
//这里内存会溢出
while(true) {
$i[] = new Test();
}
print_r($i);
}catch(\Exception $e){
print_r($e->getMessage());
}
此处执行 php test.php
控制台会报错,但是无法捕获异常,
# 错误信息
Fatal error: Allowed memory size of 2684354560 bytes exhausted (tried to allocate 1073741832 bytes) in D:\phpstudy_pro\WWW_laofan\test.php on line 14
改用 linux 日志方式 来解决
php test.php > ./log.txt
get !
评论 (0)