php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61395 Possible memory leak (in array copy?)
Submitted: 2012-03-15 04:43 UTC Modified: 2012-03-15 05:15 UTC
From: stormbyte at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.0 OS: Linux
Private report: No CVE-ID: None
 [2012-03-15 04:43 UTC] stormbyte at gmail dot com
Description:
------------
I did found on the internet some code to have test and measure memory usage. After tweaking it a bit, I discovered that when finishes (and unsetting all vars), PHP does not go back to initial memory usage which seems a memory leak.

This is the output the test script produces:

Note that memory usave in Stage 1, should be equal to memory usage in Stage 8 theoretically.

Test script:
---------------
<?php

echo "Stage 1: Mem usage is: ", memory_get_usage(), "\n";

$arr = array();

for ($i = 0; $i < 100000; ++$i) {
	$arr[] = rand();
}

echo "Stage 2: Mem usage is: ", memory_get_usage(), "\n";

$foo = 1;
$bar = 2;

echo "Stage 3: Mem usage is: ", memory_get_usage(), "\n";

$foo = $arr;
$bar = $arr;

echo "Stage 4: Mem usage is: ", memory_get_usage(), "\n";

$arr = array();

echo "Stage 5: Mem usage is: ", memory_get_usage(), "\n";

$bar[] = "hello, world";

echo "Stage 6: Mem usage is: ", memory_get_usage(), "\n";

$foo = array();

echo "Stage 7: Mem usage is: ", memory_get_usage(), "\n";

unset($arr);
unset($foo);
unset($bar);
flush();
echo "Stage 8: Mem usage is: ", memory_get_usage(), "\n";
?>

Expected result:
----------------
Expected output:
PHP 5.4.0:
Stage 1: Mem usage is: 234104
Stage 2: Mem usage is: 14883160
Stage 3: Mem usage is: 14883432
Stage 4: Mem usage is: 14883336
Stage 5: Mem usage is: 14883472
Stage 6: Mem usage is: 24732360
Stage 7: Mem usage is: 14883736
Stage 8: Mem usage is: 234104

Actual result:
--------------
PHP 5.4.0:
Stage 1: Mem usage is: 234104
Stage 2: Mem usage is: 14883160
Stage 3: Mem usage is: 14883432
Stage 4: Mem usage is: 14883336
Stage 5: Mem usage is: 14883472
Stage 6: Mem usage is: 24732360
Stage 7: Mem usage is: 14883736
Stage 8: Mem usage is: 234240

PHP 5.2.7:
Stage 1: Mem usage is: 95520
Stage 2: Mem usage is: 13945080
Stage 3: Mem usage is: 13945352
Stage 4: Mem usage is: 13945272
Stage 5: Mem usage is: 13945480
Stage 6: Mem usage is: 23794376
Stage 7: Mem usage is: 13945680
Stage 8: Mem usage is: 95656

(Much more memory used in 5.4.0?)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-15 04:45 UTC] stormbyte at gmail dot com
-Status: Open +Status: Closed
 [2012-03-15 04:45 UTC] stormbyte at gmail dot com
Forgot to unset($i) a variable and thus the false memory leak. Sorry
 [2012-03-15 04:46 UTC] stormbyte at gmail dot com
Forgot to unset($i) and thus the false memory leak. But why 5.4.0 still uses more memory is still a mistery. Sorry.
 [2012-03-15 05:15 UTC] rasmus@php.net
Compare it to PHP 5.3.x
memory_get_usage() in 5.2.x had a number of accuracy problems. If you compare 
actual memory usage using htop/top you will see it is less for 5.4.
 [2012-03-15 05:15 UTC] rasmus@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jun 26 07:01:30 2024 UTC