php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29668 Memory leak when looping an associative array
Submitted: 2004-08-14 02:17 UTC Modified: 2004-08-17 00:20 UTC
From: thecanadiancrappler at hotmail dot com Assigned:
Status: Not a bug Package: Reproducible crash
PHP Version: 4.3.8 OS: Windows XP SP 1
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: thecanadiancrappler at hotmail dot com
New email:
PHP Version: OS:

 

 [2004-08-14 02:17 UTC] thecanadiancrappler at hotmail dot com
Description:
------------
When looping numerically through an associative array, certain operations cause a massive memory leak in Apache, eating up all available ram at about 60mb/s.  Windows grinds to a halt as 1gb of Virtual Memory is gobbled up.  

Trying to echo each element of the array returns nothing as would be expected, but concatting or executing a function such a stripslashes on the elements causes the memory leak.

My php.ini differs only in max_execution time and the maximum size of POST uploads.

Reproduce code:
---------------
$array['a'] = 'Some text';
$array['b'] = "It\'s nice text";

 // prints 2
echo count($array);

 // prints nothing
echo $array[0];

 // does nothing
$array[0] = stripslashes($array[0]);


for ($i=0; $i<count($array); $i++)
{
	 // does nothing
	echo $array[$i];

	 // memory leak
	$array[$i] = 'blah' . $array[$i];

	 // memory leak
	$array[$i] = stripslashes($array[$i]);
}

Expected result:
----------------
To have no effect on the array

Actual result:
--------------
Sorry, I can't decipher your instructions for using backtrace!  Shoot the stupid guy ;o)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-17 00:20 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Each time you do $array[$i] you add a new value to the 
array. The loop goes on forever since each time the size of 
the array being calculated, move count($array) outside the 
loop. 
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Dec 04 22:00:01 2025 UTC