For Programmers: Free Programming Magazines  


Home > Archive > PHP Documentation > December 2005 > #35258 [Bgs->Opn]: preg_replace() doesn't seem to release the memory used









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author #35258 [Bgs->Opn]: preg_replace() doesn't seem to release the memory used
mfischer@php.net

2005-12-13, 8:00 am

ID: 35258
Updated by: mfischer@php.net
Reported By: mfischer@php.net
-Status: Bogus
+Status: Open
Bug Type: Documentation problem
Operating System: Linux
PHP Version: 4.4.1


Previous Comments:
------------------------------------------------------------------------

[2005-12-13 11:45:34] mfischer@php.net

No doubt. However, the ereg* family of functions does not consume
memory in the way the preg* family of functions does. So this is
actually a documentation problem it seems, re-cateogrizing.

------------------------------------------------------------------------

[2005-12-13 10:45:03] sniper@php.net

This is the same issue as all the other "PHP does not release memory"
reports. The memory reserved is never released during script run. It's
released during shutdown.

------------------------------------------------------------------------

[2005-12-13 09:53:55] mfischer@php.net

Your answer is interesting. If I take your example and insert another
var_dump() before the unset() command, like this:

<?php
var_dump(memory_get_usage());
$input = str_pad("", 50000, "foo");
var_dump(memory_get_usage());
$input = preg_replace(";foo;", "", $input);
var_dump(memory_get_usage());
unset($input); // <------------ !!
var_dump(memory_get_usage());


I get these numbers:

$ php test.php
int(15496)
int(65600)
int(115632)
int(15624)

However, there is not benefit in unsetting the return value from
preg_replace else I could remove the call to it anyway.

But maybe the example was bad. Take for example, I want to replace
'foo' with 'bar':

<?php
var_dump(memory_get_usage());
$input = str_pad("", 50000, "foo");
var_dump(memory_get_usage());
$input = preg_replace(";foo;", "bar", $input);
var_dump(memory_get_usage());


$ php test.php
int(15208)
int(65312)
int(115352)

If I want to further work with the value I have no way of saving memory
(I can't really unset the value when I need it).

I also don't quite understand the statement "memory usage doesn't
depend on the length of the data". The numbers are certainly different
when I use small strings.



------------------------------------------------------------------------

[2005-11-17 13:59:29] tony2001@php.net

Memory usage doesn't depend on the length of the data.
Try this code:
<?php
var_dump(memory_get_usage());
$input = str_pad("", 50000, "foo");
var_dump(memory_get_usage());
$input = preg_replace(";foo;", "", $input);
unset($input); // <------------ !!
var_dump(memory_get_usage());
?>

------------------------------------------------------------------------

[2005-11-17 12:22:25] mfischer@php.net

Description:
------------
preg_replace() doesn't seem to release the memory used to do its
operation.

Simple example using preg_replace():

php -r 'var_dump(memory_get_usage()); $input = str_pad("", 50000,
"foo"); var_dump(memory_get_usage()); $input = preg_replace(";foo;",
"", $input); var_dump(memory_get_usage());'

int(15216)
int(65320)
int(115352)


Simple example using ereg_replace():

php -r 'var_dump(memory_get_usage()); $input = str_pad("", 50000,
"foo"); var_dump(memory_get_usage()); $input = ereg_replace("foo", "",
$input); var_dump(memory_get_usage());'

int(15208)
int(65312)
int(15312)

Maybe this is just a documentation issue, but than it should be noted.
The documentation says that preg_ is faster, which it is, and the
documentation somewhere mentiones a regex cache, but it doesn't say
that the amount of memory grows by the size of the input sring.

It's an interesting behaviour which should be considered when memory is
an issue.



------------------------------------------------------------------------


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







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com