On 03/06/2007 12:34 AM, Brian Moon wrote:
> Ok, I am no C coder and I don't claim to be very smart about low level
> parts of PHP. But, IMO, this is bug.
>
> http://bugs.php.net/bug.php?id=36924
>
> I have created a test script that shows the insanity of this bug:
>
> <?php
>
> $str = "This is a medium length string";
>
> $start = memory_get_usage();
> for($x=1;$x<=100;$x++){
> echo $str.": (3.5): ".time()."\n";
> }
> $first_growth = number_format(memory_get_usage() - $start);
>
> $start = memory_get_usage();
> for($x=1;$x<=100;$x++){
> echo "$str: (3.5): ".time()."\n";
> }
> $growth = number_format(memory_get_usage() - $start);
>
>
> echo "first growth: $first_growth\nsecond growth: $growth\n";
>
> ?>
Number of iterations | First growth | Second growth | Peak usage
100 480 bytes ~5Kb ~73Kb
100 000 556 bytes ~32Kb ~100Kb
1 000 000 556 bytes ~32Kb ~100Kb
Tested with PHP 5.2.1 and 5.2.2-dev on Linux (with --disable-debug).
> Now, you turn that into a script that is going to loop millions of
> times, building strings to build data and run for hours. The next thing
> you know, your cron job is using 450MB of memory and you try to figure
> out what YOU did wrong. Hours later you find this little jewel waiting
> for you and realize you did not do anything wrong. You just used the
> language.
Yup, the language stole ~32Kb of your memory and used it to speedup the allocation of small chunks.
> Everyone ignored my last email about memory waste and large arrays. I
> can only assume that you will ignore this one too.
> I am starting to see why Stefan became so bitter.
> I have always tried to squash the "myth" that putting variables inside
> strings in PHP was bad, but now, I think I will flip on that.
> I tested as far back as PHP 4.3.9 and as new as 5.2.1.
--
Wbr,
Antony Dovgal