PHP完成后不清除内存


PHP not clearing RAM when finished

下面是我试图完成的代码,以查看PBX上的日志。它运行良好,对日志文件进行排序,显示所有数据,等等。问题是,一旦完成,它会在内存中保留大量数据(几百MB)。我想把它清理干净,但不知道怎么做。如果它运行几次,它会从服务器中吸收所有的RAM,我们就会开始出现错误。提前感谢!

<!DOCTYPE html>
<html>
<title>Call Search</title>
<body>
<?php
function getBetween($var1, $var2, $pool)
{
        $temp1 = strpos($pool, $var1) + strlen($var1);
        $result = substr($pool, $temp1, strlen($pool));
        $dd=strpos($result, $var2);
        if($dd == 0){$dd = strlen($result);}
        return substr($result, 0, $dd);
}
if(!isset($_POST['phonenum']) && !isset($_POST['WantedCallID']))
{
        echo '<form action="test.php" method="post">';
            echo '<h4>Please enter the number you wish to search for...</h4><input     type="text" name="phonenum">';
    echo '<input type="submit" value="Go">';
    echo '</form>';
}
else if(isset($_POST['WantedCallID']))
{
   $counter = 0;
   $logfile="calllog";
   while (file_exists("/var/log/".$logfile))
   {
      if($counter==0){$logfile="calllog";}
      else{$logfile="callog.".$counter;}
      $path = "/var/log/".$logfile;
      $logtoarray = file($path);
      foreach ($logtoarray as $linenumber => $line)
      {
         if(strpos($line, "[".$_POST['WantedCallID']."]") !== false){echo $line."    <br>";}
      }
   $counter++;
   }
}
else
{
   $counter = 0;
   $logfile="calllog";
   $arrayCounter = 0;
   while (file_exists("/var/log/".$logfile))
   {
      if($counter == 0){$logfile = "calllog";}
      else{$logfile="calllog.".$counter;}
      $path = "/var/log/".$logfile;
      if(file_exists($path))
      {
         $logtoarray = file($path);
         $oldCallId = "";
         $currentCallId = "";
         foreach ($logtoarray as $linenumber => $line)
         {
             if(strpos($line, $_POST['phonenum']) !== false && strpos($line, "VERBOSE[") !== false)
             {
                    $currentCallId = getBetween("VERBOSE[", "]", $line);
                if($currentCallId !== $oldCallId)
                {
                   $callIdArray[$arrayCounter] = "Date: ".substr($line, 0, 22)." Call ID:".$currentCallId."'n";
                   echo "Date: ".substr($line, 0, 22)." Call ID:".$currentCallId."<br>";
               $oldCallId = $currentCallId;
            }
        }
        $arrayCounter++;
     }
  }
  else{break;}
  $counter++;
   }
   echo '<form action="test.php" method="post">';
   echo '<h4>Please enter the call ID you wish to search for...</h4><input type="text" name="WantedCallID">';
   echo '<input type="submit" value="Go">';
   echo '</form>';
}
$variables = array_keys(get_defined_vars());
foreach($variables as $variable) {
    unset(${"$variable"});
}
unset($callIdArray);
?>
</body>
</html>

假设您通过Apache服务器运行此脚本。这是一个相当常见的问题,尽管我更经常在调整图像大小时看到它。

一旦Apache子进程(运行PHP的进程)由于使用了大量数据而增加了内存量,或者更常见的是PHP,它就没有办法将内存返回给操作系统。

相反,每个子进程都有MaxRequestsPerChild设置。这可以设置为0 -永不重启。如果内存膨胀是一个问题,那么将其设置为几百或几千可能会有用。在这么多请求之后,重新启动apache子进程,因此捕获的所有内存将返回给操作系统,如果需要,还会启动一个新的子进程。当进程重新启动时,请求可能会花费更长的时间,但是释放内存会更有用。

下面是对代码的一个简单修改,它将每次一行读取日志文件,而不是将整个文件读取到内存中。

while (file_exists("/var/log/".$logfile))
{
    if($counter == 0){$logfile = "calllog";}
    else{$logfile="calllog.".$counter;}
    $path = "/var/log/".$logfile;
    if(file_exists($path))
    {
        $fh = fopen($path, 'r'); //change
        $linenumber = 0; //addition
        $oldCallId = "";
        $currentCallId = "";
        while( $line = fgets($fh) ) //change
        {
            $linenumber++; //addition
            if(strpos($line, $_POST['phonenum']) !== false && strpos($line, "VERBOSE[") !== false)
            {
                $currentCallId = getBetween("VERBOSE[", "]", $line);
                if($currentCallId !== $oldCallId)
                {
                    $callIdArray[$arrayCounter] = "Date: ".substr($line, 0, 22)." Call ID:".$currentCallId."'n";
                    echo "Date: ".substr($line, 0, 22)." Call ID:".$currentCallId."<br>";
                    $oldCallId = $currentCallId;
                }
            }
            $arrayCounter++;
        }
        fclose($fh); //addition
    }
}