php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28940 Low level string manipulation memory leak
Submitted: 2004-06-28 09:58 UTC Modified: 2011-11-07 08:22 UTC
From: fenn_b at smktech dot com dot au Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.3.7 OS: Linux
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: fenn_b at smktech dot com dot au
New email:
PHP Version: OS:

 

 [2004-06-28 09:58 UTC] fenn_b at smktech dot com dot au
Description:
------------
There appears to be a fairly severe low-level memory leak in the PHP string parser that occurs when variables included inline with strings are expanded out to their values.

This seems to be causing large scale memory leakage on long scripts (ie: CLI type ones that run for a while) in various PEAR modules, and other countless third party programs.

Took me a while to narrow down what looked like a DB issue to this :)

I have replicated this in PHP 4.3.6 and 4.3.7 with both CLI and dynamic module SAPIs.

Configure info:
----------------------------
 './configure' '--with-mysql' '--with-pgsql' '--enable-track-vars' '--with-gd' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--with-zlib=/usr' '--with-xmlrpc' '--with-xml' '--enable-exif' '--with-gettext' '--with-imap' '--enable-ftp' '--with-mcrypt' '--with-ldap' '--enable-sockets' '--enable-memory-limit' '--with-mcal=/usr' '--with-dom' '--with-curl' '--enable-force-cgi-redirect' '--with-expat-dir=/usr' '--with-freetype-dir=/usr' '--enable-simplexml'
----------------------------

As you can see by the code below (neatness somewhat ruined to fit in the 20 line limit), in every iteration of the loop (and subsequent call of the *leakMemory() functions), the $discardString should be constructed, then go out of scope and release the memory.

In the case where inline variable substitution is used, the memory appears to get eaten, and never given back. In the example, only 320 bytes are being used per iteration, but in a complex system, with many calls made (ie: PEAR::DB type stuff), I've seen up to 200KB or so used per call.

If someone could have a look at this, it would be really appreciated. It's causing me some pain at the moment :)

Thanks very much,

  Fenn.

Reproduce code:
---------------
$string1 = "string1"; $string2 = "string2";
function noLeakMemory() {
    global $string1, $string2;
    $discardString = "string1 " . $string1 . " string2 " . $string2;
}
function leakMemory() {
    global $string1, $string2;
    $discardString = "string1 $string1 string2 $string2";
}
for ($loop = 0; $loop < 10; $loop ++) {
    for ($innerLoop = 0; $innerLoop < 10; $innerLoop ++) {
        noLeakMemory();
    }
    print "Memory usage (no leak): " . memory_get_usage() . "\n";
}
for ($loop = 0; $loop < 10; $loop ++) {
    for ($innerLoop = 0; $innerLoop < 10; $innerLoop ++) {
        leakMemory();
    }
    print "Memory usage (leak): " . memory_get_usage() . "\n";
}


Expected result:
----------------
What should be output (approx):
------------------
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (leak): 33048
Memory usage (leak): 33048
Memory usage (leak): 33048
Memory usage (leak): 33048
Memory usage (leak): 33048
Memory usage (leak): 33048
Memory usage (leak): 33048
Memory usage (leak): 33048
Memory usage (leak): 33048
Memory usage (leak): 33048
------------------

Actual result:
--------------
Output for script looks like this for me:
------------------
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (no leak): 32728
Memory usage (leak): 33048
Memory usage (leak): 33368
Memory usage (leak): 33688
Memory usage (leak): 34008
Memory usage (leak): 34328
Memory usage (leak): 34648
Memory usage (leak): 34968
Memory usage (leak): 35288
Memory usage (leak): 35608
Memory usage (leak): 35928
------------------

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-28 12:53 UTC] derick@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

This is actually a feature... PHP reuses previously allocated variable containers. Change your loop count from 10 to 1000 and you see that it stabilizes.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 15:01:29 2024 UTC