Php 因过度使用资源而终止进程
今天我在console中测试了一些脚本,这些脚本在服务器端似乎有问题,对于某个文件,我得到了以下建议: 哎呀!您的一个进程php,pid 15278刚刚因为过度使用资源而被终止。 有关详细信息,请联系DreamHost支持 好的,明白了,我应该升级我的主机,但我不清楚为什么有时候会发生这种情况,显然资源使用率要高得多 处理导致错误的文件的代码段生成otuput:Php 因过度使用资源而终止进程,php,process,dreamhost,Php,Process,Dreamhost,今天我在console中测试了一些脚本,这些脚本在服务器端似乎有问题,对于某个文件,我得到了以下建议: 哎呀!您的一个进程php,pid 15278刚刚因为过度使用资源而被终止。 有关详细信息,请联系DreamHost支持 好的,明白了,我应该升级我的主机,但我不清楚为什么有时候会发生这种情况,显然资源使用率要高得多 处理导致错误的文件的代码段生成otuput: Iteration 1 Start Memory Usage At the Start: 31,179.26Kbytes //Got
Iteration 1 Start
Memory Usage At the Start: 31,179.26Kbytes //Got with memory_get_usage
Memory Usage At the End: 26,350.64Kbytes
Duration:14
Iteration 2 Start
Yikes! One of your processes (php, pid 15278) was just killed for excessive resource usage.
Please contact DreamHost Support for details.
但是,使用另一个更大的文件,我会得到以下日志:
Iteration 1 Start
Memory Usage At the Start: 132,115.70Kbytes
Memory Usage At the End: 26,350.64Kbytes: 129,268.95Kbytes
Duration:34
Iteration 2 Start
Memory Usage At the Start: 131,368.04Kbytes
Memory Usage At the End: 129,287.41Kbytes
Duration:36
....
等等
问题是,为什么第一个文件总是出现同样的错误,似乎占用更少的内存,而第二个文件完成了12次迭代却没有一个问题?猜猜看
更新:添加代码
抱歉搞得一团糟。心灵感应不起作用,请发代码。以前没有发代码,因为我认为这会分散我问的问题的注意力,也帮不了什么忙。我想我迟早会需要升级我的托管计划,我更好奇的是,为什么用更少的资源我没有得到错误,但我会编辑并发布代码,顺便说一句,将块大小从5000个减少到250个,但我还不知道为什么会有不同。糟糕的是,它没有解决问题,只是把它拖得太久,让我觉得它已经修好了。当它进入第52次迭代时,它又死掉了。未设置之前的内存使用率一直以很小的速度增长,未设置之后又恢复正常,这有点奇怪。phpexcel阅读器似乎需要将文件打开到它要读取的行
foreach ( $worksheets as $worksheet ){
$this->ultimo_apunte = NULL;
for ( $i=1; $i < $worksheet["totalRows"]; $i+=self::$chunk_size ){
echo "\Iteration".(($i-1)/250+1);
$comienzo_iteracion = time();
$filter = new ExcelReadFilter($i, self::$chunk_size);
$reader->setReadFilter($filter);
$objPHPExcel = $reader->load($excel_file);
$objPHPExcel->setActiveSheetIndexByName($worksheet["worksheetName"]);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,false,true);
$c = 0;
foreach ( $sheetData as $rowIndex => $row ){
if (!isset($apunte)) {
$apunte = Apunte::conFila($row);
if ( $apunte->completo() ){
$apunte->ejercicio = "N/A";
fwrite($f, $apunte->insert_stmt());
$this->ultimo_apunte = $apunte;
unset($apunte);
}
}
else{
$apunte->completar($row);
if ( $apunte->completo() ){
$apunte->ejercicio = "N/A";
fwrite($f, $apunte->insert_stmt());
$this->ultimo_apunte = $apunte;
unset($apunte);
}
}
}
echo "\nMemory Usage At the Start: ".number_format((memory_get_usage()/1024), 2)."Kbytes";
unset($sheetData);
unset($objPHPExcel);
echo "\nMemory Usage At the End: ".number_format((memory_get_usage()/1024), 2)."Kbytes";
$fin_iteracion = time();
$it_duration = $fin_iteracion - $comienzo_iteracion;
$iteraciones_completadas+=1;
if ( !$process ) {
$process = new stdClass;
$process->start_time = date("Y-m-d");
}
if ( $iteraciones_completadas == 0 ){
$process->ellapsed_time = 0;
}else{
$process->ellapsed_time = $fin_iteracion - strtotime($process->start_time);
}
$process->estimated_time = $process->ellapsed_time+$it_duration*($num_iteraciones - $iteraciones_completadas);
echo "\nDuration:".$it_duration."";
//log_string(ob_get_contents());
//ob_clean();
if ( method_exists($process, "saveToFile" )) $process->saveToFile();
}
}
fclose($f);
global $db;
$sql = "
LOAD DATA LOCAL INFILE '".$db->real_escape_string($sql_file)."'
INTO TABLE apuntes
FIELDS TERMINATED BY ','
ENCLOSED BY '".'"'."'
ESCAPED BY '\\\'
LINES TERMINATED BY '\\n'
(id_diario, fecha, asiento, apunte, cuenta, descripcion, concepto, debe, haber, es_apertura, es_cierre, ejercicio);
";
error_reporting(0);
$db->query($sql);