-1

I have a simple PHP script running as a cron job, it's purpose is to check the headers of a webpage and report if various items have changed.

I have used various versions of this script in the past and it has always worked fine, but now I'm using it on a server that sends out warnings by email when scripts are using more than 100Mb of memory (apart from whitelisted scripts of course), and surprisingly my header-checking script is occasionally reporting large amounts of memory usage at least a few times a day.

The script is running every 5 minutes, and the memory problem is happening 5 or 6 times a day - it does seem to resolve itself, but I would like to stop it from using this large amount of memory occasionally for such a simple job.

The warning emails are reporting usage of around 290Mb for this simple script :

<?php

  $message = "";

  function array_find ($needle, $haystack, $search_keys = false) { 
    if (!is_array($haystack)) return false;
    foreach ($haystack as $key => $value) {
      $what = ($search_keys) ? $key : $value;
      if (strpos($what, $needle) !== false) return $key;
    }
    return false;
  }

  $url = 'http://www.EXAMPLE.com/';

  $haystack = get_headers($url);
  $needle = "PHPSESSID";

  if (!((array_find($needle, $haystack)) != '')) {
    $message .= "No PHPSESSID in headers";
  }

  if (!(strpos($haystack[0], '200'))) {
    $message .= "No 200 OK";
  }

  if ($message != '') {
    mail("example@example.com", "Headers Changed", $message, "From: Example <example@example.com>");
  }

  exit;

?>

The server is a VPS running CENTOS 5.8, Apache 2.2.16, PHP 5.3.3

Thanks for any assistance.

4

3 回答 3

2

或许您每天只需要 5 到 6 次大干草堆?

PHP 是一个内存饥渴的野兽。这是事实。在处理相对较大的数据数组时,内存消耗可能会变得不那么明显。

有一篇有趣的文章,名为“PHP 数组(和值)有多大?(提示:大!)”强烈推荐阅读!

于 2012-04-05T11:45:09.897 回答
1

尝试仅使用 get_headers($url) 和 memory_get_usage() 创建一个文件并查看结果...

<?php 
$url = "http://www.example.com";
get_headers($url);
echo memory_get_usage();
?>

根据我的经验,PHP >= 5 上的 get_headers() 会占用大量内存。您可以查看文章“ get_headers() 函数导致内存泄漏

PS:也可以使用xDebug来跟踪脚本的内存使用情况

于 2012-07-18T07:39:00.837 回答
1

也许在每个主要部分之后尝试做 memory_get_usage() 。这应该可以帮助您缩小范围。

于 2012-04-05T11:58:43.613 回答