ob_flush() possible memory leak in Apache

From: Date: Tue, 19 Dec 2006 02:33:17 +0000
Subject: ob_flush() possible memory leak in Apache
Groups: php.general 
Request: Send a blank email to php-general+get-245993@lists.php.net to get a copy of this message
Server Specs ------------ CentOS 4.4 Apache 2.0.52 (httpd-2.0.52-28.ent.centos4) PHP 5.1.6 (php-5.1.6-1.2.centos) PHP running as Apache Module. Simple script reads a file in (mp3 or other binary file) and spits out to browser. Upon completion, memory consumption of the Apache child rises dramatically and CPU usage spikes. Same is true under PHP 5.2.0. Removing ob_flush and using ini_set("output_buffering = Off"); eliminates the problem, but causes others (browser doesn't appear to see the headers). Just want to make sure I'm using the function correctly before filing a bug report. Offending code example below, just add mp3 file. Use 'top -u apache' to monitor httpd process, RSS (memory) will jump at the end of download. You can safely kill that child process ID without having to restart the entire server. Chris <?php $filename = "test.mp3"; if ($handle = fopen($filename,"r")) {
       header("Content-Type: application/octet-stream");
       header("Pragma: public");
       header("Expires: 0");
       header("Cache-Control: public, must-revalidate, post-check=0, " .
         "pre-check=0");
       header("Content-Description: File Transfer");
       header("Content-Disposition: attachment; filename=" . $filename);
       header("Content-Transfer-Encoding: binary");
       header("Content-Length: " . filesize($filename));
       while (($content = fread($handle,1024)) !== FALSE) {
         echo $content;
         ob_flush();
         flush();
       }
     }
     fclose($handle);
?>

« previous php.general (#245993) next »