php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52536 array_shift memory leak.
Submitted: 2010-08-04 22:43 UTC Modified: 2010-08-05 00:10 UTC
From: aikar at aikar dot co Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.3 OS: CentOS 5.3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: aikar at aikar dot co
New email:
PHP Version: OS:

 

 [2010-08-04 22:43 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-05 00:09 UTC] aikar at aikar dot co
-Status: Open +Status: Closed
 [2010-08-05 00:09 UTC] aikar at aikar dot co
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.
 [2010-08-05 00:10 UTC] felipe@php.net
-Status: Closed +Status: Bogus -Package: Performance problem +Package: Arrays related
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 21:01:30 2024 UTC