PHP完成后不清除RAM

问题描述 投票:1回答:2

下面是我试图完成的代码,以查看我们PBX上的日志。 它运行得很好,对日志文件进行分类,显示所有数据等。 问题是,一旦完成,它就在内存中保留了大量的数据(几百MB)。 我需要在它完成后将其清除,但又想不出如何清除。 如果它运行了几次,就会吸走服务器的所有内存,我们就会开始出现错误。 先谢谢你

<!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>
php memory ram
2个回答
0
投票

这里对你的代码做了一个简单的修改,可以一次一行一行地读取日志文件,而不是把整个文件读到内存中。

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
    }
}

2
投票

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

一旦Apache子进程(运行PHP的进程)因为使用了大量的数据而增加了内存量,它就没有方法把内存返回给操作系统。

相反,每个子进程都有 MaxRequestsPerChild 设置。这可能被设置为 0 - 永不重启。如果膨胀的内存大小是个问题,那么把这个设置为几百,或者上千,可能会有用。在这么多的请求之后,apache子程序就会被重启,因此所有被捕获的内存都会返回给操作系统,如果需要的话,新的子程序会被启动。虽然重启进程的时候,请求的时间可能会长一点,但是释放内存的作用会更大。

© www.soinside.com 2019 - 2024. All rights reserved.