Hi Forum

I have written a few PHP Scripts. I am using XAMPP with PHP 5.2.8 and Apache 2.2.1 on Windows 2003.

Every time I open a PHP script in my browser RAM usage is increased around 4-5 MB. After two days the apache process grew to 2 GB RAM usage and crashes!

I have tried to free the result set (mssql_free_result($result)😉. Do I have to manually unset() all used variables? Why? I have always developed on linux and never seen such a behavior.

Thanks in advance
Gunnar

    Is it a specific PHP script that you see exhibits this behavior? If so, can you post it here for us to see?

      Hi

      I found the problem - after one day debugging - but cannot solve it!

      All of my scripts start with

      <?php
      include("config.php");
      ?>

      All my functions are in this config.php file.

      The datediff function in this file is the problem. If I start this function one time in a script the apache process takes 300kb and don't give it back! And I use this function very often ;-)

      Does someone know why? Is it because the function is in another file? Is it because of the include?

      Maybe someone can help with this.
      Best regards
      Gunnar

      function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
          /*
          $interval can be:
          yyyy - Number of full years
          q - Number of full quarters
          m - Number of full months
          y - Difference between day numbers
              (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
          d - Number of full days
          w - Number of full weekdays
          ww - Number of full weeks
          h - Number of full hours
          n - Number of full minutes
          s - Number of full seconds (default)
          */
      
      if (!$using_timestamps) {
          $datefrom = strtotime($datefrom, 0);
          $dateto = strtotime($dateto, 0);
      }
      $difference = $dateto - $datefrom; // Difference in seconds
      
      switch($interval) {
      
      case 'yyyy': // Number of full years
      
          $years_difference = floor($difference / 31536000);
          if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
              $years_difference--;
          }
          if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
              $years_difference++;
          }
          $datediff = $years_difference;
          break;
      
      case "q": // Number of full quarters
      
          $quarters_difference = floor($difference / 8035200);
          while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
              $months_difference++;
          }
          $quarters_difference--;
          $datediff = $quarters_difference;
          break;
      
      case "m": // Number of full months
      
          $months_difference = floor($difference / 2678400);
          while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
              $months_difference++;
          }
          $months_difference--;
          $datediff = $months_difference;
          break;
      
      case 'y': // Difference between day numbers
      
          $datediff = date("z", $dateto) - date("z", $datefrom);
          break;
      
      case "d": // Number of full days
      
          $datediff = floor($difference / 86400);
          break;
      
      case "w": // Number of full weekdays
      
          $days_difference = floor($difference / 86400);
          $weeks_difference = floor($days_difference / 7); // Complete weeks
          $first_day = date("w", $datefrom);
          $days_remainder = floor($days_difference % 7);
          $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
          if ($odd_days > 7) { // Sunday
              $days_remainder--;
          }
          if ($odd_days > 6) { // Saturday
              $days_remainder--;
          }
          $datediff = ($weeks_difference * 5) + $days_remainder;
          break;
      
      case "ww": // Number of full weeks
      
          $datediff = floor($difference / 604800);
          break;
      
      case "h": // Number of full hours
      
          $datediff = floor($difference / 3600);
          break;
      
      case "n": // Number of full minutes
      
          $datediff = floor($difference / 60);
          break;
      
      default: // Number of full seconds (default)
      
          $datediff = $difference;
          break;
      }    
      
      return $datediff;
      
      }

        So you've verified that by simply not calling this one function, you no longer have memory issues?

        It sounds like you may have found a memory leak in PHP itself. Which version of PHP are you using?

          10 days later

          Hi
          So what do I have to do? Update to a newer version?

          My PHP version is 5.2.8 as I wrote above.

          Best regards
          Gunnar

            groetschel;10947810 wrote:

            My PHP version is 5.2.8 as I wrote above.

            If this actually were a PHP bug and not your code's fault (not really sure how it could be, though it might help to see how you call this function), and if you were accurately observing a memory leak, then the first thing that the PHP developers would ask you to do is to upgrade to the latest version of PHP (no sense in bothering them with a bug that doesn't exist anymore :p).

            As such, you might try upgrading to the latest version of the 5.2 branch; at the time of this post, version 5.2.13 is the latest stable release. If that still doesn't work, try upgrading to the latest stable release of the 5.3 branch (5.3.2 at this time).

            If upgrading to 5.3.2 still doesn't resolve the apparent memory leak, it might be time to open a bug report at http://bugs.php.net.

            Can you attach the following items to your next post so that I can try to reproduce the memory leak? The items I would need are:

            1. An example script that implements the function you showed us and exhibits the memory leak behavior? Don't worry if it connects to a database - I just want to see how the function is being called

            2. The output of phpinfo() (you can copy-paste it into a .txt file)

            3. Your php.ini file

            4. Your httpd.conf file

            (Don't forget to remove any sensitive data - e.g. user credentials/passwords - before attaching files!)

            EDIT: Also, I couldn't find Apache 2.2.1 (only 2.2.0, and 2.2.2 on the archive site). I'll try mimicking your setup using Apache 2.2.2 (and/or 2.2.0), but perhaps you might try upgrading to the latest stable release of Apache 2.2 (presently 2.2.15) as well?

              Write a Reply...