[PHP-BUG] Bug #52536 [NEW]: array_shift memory leak.

2010-08-04 Thread aikar at aikar dot co
From: 
Operating system: CentOS 5.3
PHP version:  5.3.3
Package:  Performance problem
Bug Type: Bug
Bug description:array_shift memory leak.

Description:

array_shift is causing a memory leak when used.



Take the following code:



if (count($write->writeBuffer))

{

$data = array_shift($write->writeBuffer);

$written = fwrite($stream, $data);

if ($written !== FALSE)

{

if ($written < strlen($data))

{

array_unshift($write->writeBuffer, substr($data,
$written));

}

}

}



When running under as a daemon process, memory will continue to rise with
this 

code.



However, simply changing the code to:



if (count($write->writeBuffer))

{

//print_r($write);

$data = reset($write->writeBuffer);

$key = key($write->writeBuffer);

//echo "writing data: $data\n";

$written = fwrite($stream, $data);

if ($written !== FALSE)

{

if ($written < strlen($data))

{

$write->writeBuffer[$key] = substr($data, $written);

} else

{

unset($write->writeBuffer[$key]);

}

}

}



Fixes the problem and no longer leaks and stays at a steady number.





Test script:
---
requires a long running process that repeatedly calls array_shift to
demonstrate

Expected result:

Memory to be freed that is used for the array_shift operation.

Actual result:
--
Memory is not freed upon use and contiously climbs in total memory
consumption by 

the PHP process.

-- 
Edit bug report at http://bugs.php.net/bug.php?id=52536&edit=1
-- 
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=52536&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=52536&r=trysnapshot53
Try a snapshot (trunk):  
http://bugs.php.net/fix.php?id=52536&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=52536&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=52536&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=52536&r=alreadyfixed
Need backtrace:  
http://bugs.php.net/fix.php?id=52536&r=needtrace
Need Reproduce Script:   
http://bugs.php.net/fix.php?id=52536&r=needscript
Try newer version:   
http://bugs.php.net/fix.php?id=52536&r=oldversion
Not developer issue: 
http://bugs.php.net/fix.php?id=52536&r=support
Expected behavior:   
http://bugs.php.net/fix.php?id=52536&r=notwrong
Not enough info: 
http://bugs.php.net/fix.php?id=52536&r=notenoughinfo
Submitted twice: 
http://bugs.php.net/fix.php?id=52536&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=52536&r=globals
PHP 4 support discontinued:  http://bugs.php.net/fix.php?id=52536&r=php4
Daylight Savings:http://bugs.php.net/fix.php?id=52536&r=dst
IIS Stability:   
http://bugs.php.net/fix.php?id=52536&r=isapi
Install GNU Sed: 
http://bugs.php.net/fix.php?id=52536&r=gnused
Floating point limitations:  
http://bugs.php.net/fix.php?id=52536&r=float
No Zend Extensions:  
http://bugs.php.net/fix.php?id=52536&r=nozend
MySQL Configuration Error:   
http://bugs.php.net/fix.php?id=52536&r=mysqlcfg



Bug #52536 [Opn->Csd]: array_shift memory leak.

2010-08-04 Thread aikar at aikar dot co
Edit report at http://bugs.php.net/bug.php?id=52536&edit=1

 ID: 52536
 User updated by:aikar at aikar dot co
 Reported by:aikar at aikar dot co
 Summary:array_shift memory leak.
-Status: Open
+Status: Closed
 Type:   Bug
 Package:Performance problem
 Operating System:   CentOS 5.3
 PHP Version:5.3.3
 Block user comment: N

 New Comment:

Sorry, upon furthur review i noticed the memory does stop rising
eventually, and 

found my memory "leak" to be a flaw somewhere else that took it up
high.



closing since this isnt a bug.


Previous Comments:

[2010-08-04 22:43:22] aikar at aikar dot co

Description:

array_shift is causing a memory leak when used.



Take the following code:



if (count($write->writeBuffer))

{

$data = array_shift($write->writeBuffer);

$written = fwrite($stream, $data);

if ($written !== FALSE)

{

if ($written < strlen($data))

{

array_unshift($write->writeBuffer, substr($data,
$written));

}

}

}



When running under as a daemon process, memory will continue to rise
with this 

code.



However, simply changing the code to:



if (count($write->writeBuffer))

{

//print_r($write);

$data = reset($write->writeBuffer);

$key = key($write->writeBuffer);

//echo "writing data: $data\n";

$written = fwrite($stream, $data);

if ($written !== FALSE)

{

if ($written < strlen($data))

{

$write->writeBuffer[$key] = substr($data,
$written);

} else

{

unset($write->writeBuffer[$key]);

}

}

}



Fixes the problem and no longer leaks and stays at a steady number.





Test script:
---
requires a long running process that repeatedly calls array_shift to
demonstrate

Expected result:

Memory to be freed that is used for the array_shift operation.

Actual result:
--
Memory is not freed upon use and contiously climbs in total memory
consumption by 

the PHP process.






-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52536&edit=1