From: morgan at mindviz dot com
Operating system: RedHat Linux 8.0
PHP version: 4.3.3
PHP Bug Type: CGI related
Bug description: CLI application not freeing used memory even with unset
Description:
------------
Basically I'm writing a php-CLI server application that
will never exit unlike an apache process. I have been
attempting to find out why slowly but surely the memory
usage of my CLI app steadily goes up until it has
exhausted all my available memory.
The server app will read in large data sets at times
and then attempts to free those with the unset
function. With further inspection it seems that even
though unset is called the script still has a hold on
or uses the allocated memory and never returns it to
the system.
The code below is a small script that I put together to
show the issue I am seeing, it is quite easy to
reproduce the problem.
As with my server application if the script below was
run forever and the max value of $z in the loop was set
extremely high it would run until all the system's
memory was used up, since unset is not actually
returning any of the used memory for the test_array
back to the system.
Reproduce code:
---------------
#!/root/php-4.3.3/php -n
<?php
$my_pid = getmypid();
echo "GOT PID: ".`ps -eo %mem,rss,pid | grep $my_pid`."\n";
for($z = 0; $z < 10; $z++) {
$test_array = array();
// Build a Test 5x(1000*$z) array, will show incremental mem usage
and after unset
// line will test_array's memory will not be free'd
for($i = 0; $i < 5; $i++) {
for($j = 0; $j < 1000*$z; $j++) {
$test_array[$i][$j] = "testing $i $j\n";
}
}
echo "$z: MEMORY USAGE BEFORE UNSET ( % KB PID ) : ".`ps -eo
%mem,rss,pid | grep $my_pid`;
unset($test_var);
echo "$z: MEMORY USAGE AFTER UNSET ( % KB PID ) : ".`ps -eo
%mem,rss,pid | grep $my_pid`."\n";
}
exit;
?>
Expected result:
----------------
GOT PID: 0.1 1524 3178
0: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.1 1532
3178
0: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1524
3178
1: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.2 2132
3178
1: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1524
3178
2: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.3 2740
3178
2: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1524
3178
3: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.3 3368
3178
3: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1524
3178
4: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.4 3952
3178
4: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1524
3178
5: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.5 4620
3178
5: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1524
3178
6: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.5 5204
3178
6: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1524
3178
7: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.6 5792
3178
7: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1524
3178
8: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.7 6376
3178
8: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1524
3178
9: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.7 7124
3178
9: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1524
3178
Actual result:
--------------
GOT PID: 0.1 1524 3178
0: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.1 1532
3178
0: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.1 1532
3178
1: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.2 2132
3178
1: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.2 2132
3178
2: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.3 2740
3178
2: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.3 2740
3178
3: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.3 3368
3178
3: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.3 3368
3178
4: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.4 3952
3178
4: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.4 3952
3178
5: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.5 4620
3178
5: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.5 4620
3178
6: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.5 5204
3178
6: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.5 5204
3178
7: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.6 5792
3178
7: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.6 5792
3178
8: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.7 6376
3178
8: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.7 6376
3178
9: MEMORY USAGE BEFORE UNSET ( % KB PID ) : 0.7 7124
3178
9: MEMORY USAGE AFTER UNSET ( % KB PID ) : 0.7 7124
3178
--
Edit bug report at http://bugs.php.net/?id=25853&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=25853&r=trysnapshot4
Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=25853&r=trysnapshot5
Fixed in CVS: http://bugs.php.net/fix.php?id=25853&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=25853&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=25853&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=25853&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=25853&r=support
Expected behavior: http://bugs.php.net/fix.php?id=25853&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=25853&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=25853&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=25853&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=25853&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=25853&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=25853&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=25853&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=25853&r=float