关于PHP内存溢出问题的解决方法
前端之家收集整理的这篇文章主要介绍了
关于PHP内存溢出问题的解决方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_0@
<FONT style="COLOR: #ff0000">一.内存溢出解决方案
在做数据
统计分析时,经常会遇到大数组,可能会发生内存溢出,这里
分享一下我的
解决方案。还是用例子来说明这个问题,如下:
假定日志中存放的记录数为500000条,那么
解决方案如下:
<div class="codetitle">
<a style="CURSOR: pointer" data="11243" class="copybut" id="copybut11243" onclick="doCopy('code11243')"> 代码如下: <div class="codebody" id="code11243">
ini_set(‘memory_limit','64M'); //重置
PHP可以使用的内存大小为64M,一般在远程主机上是不能
修改PHP.ini
文件的,只能通过程序设置。注:在safe_mode(安全模式)下,ini_set失效
set_time_limit(600);//设置超时限制为6分钟
$farr = $Uarr = $Marr = $IParr = $data = $_sub = array();
$spt = ”$@#!$”;
$root = ”/Data/webapps/VisitLog”;
$path = $dpath = $fpath = NULL;
$path = $root.”/”.date(“Y-m”,$timestamp);
$dpath = $path.”/”.date(“m-d”,$timestamp);
for($j=0;$j<24;$j++){
$v = ($j < 10) ? ”0″.$j : $j;
$gpath = $dpath.”/”.$v.”.
PHP”;
if(!file_exists($gpath)){
continue;
} else {
$arr = file($gpath);////将
文件读入数组中
array_shift($arr);//移出第一个单元-》<?php exit;?>
$farr = array_merge($farr,$arr);
unset($arr);
}
}
if(empty($this->farr)){
echo ”
没有相关记录!”;
exit;
}
while(!empty($farr)){
$_sub = array_splice($farr,10000); //每次取出$farr中1000个
for($i=0,$scount=count($_sub);$i<$scount;$i++){
$arr = explode($spt,$_sub[$i]);
$Uarr[] = $arr[1]; //vurl
$Marr[] = $arr[2]; //vmark
$IParr[] = $arr[3].” |$nbsp;”.$arr[1]; //IP
}
unset($_sub);//用完及时销毁
}
unset($farr);