On Mon, 2006-08-14 at 13:16 -0400, Adam Zey wrote:
> I was writing a shell script in PHP (4.4.2) that dealt with a rather
> large array. To figure out what I needed the new memory limit to be, I
> did a memory_get_usage() at the end of my script, and came up with about
> 5.5MB. I then set the memory limit to 8MB.
>
> When I tried to run it, the script ran out of memory on the line:
>
> $numwords = count($words);
>
> However, when I switched to simply incrementing $numwords every time I
> added an element to $words, the memory limit of 8MB was fine.
>
> So my question is, if PHP does copy-on-write, why does PHP make a copy
> of an array when you use count() on it, which should NOT be modifying
> the array?
For some reason the memory_get_usage() function wouldn't appear in my
PHP compilation even after using the --enable-memory-limit flag, and
rather than dig very deep, I whipped up the following script to test
your issue (under PHP 4.2.2):
<?php
//echo 'Mem Usage: '.memory_get_usage()."\n";
$foo = array();
for( $i = 0; $i < 10000000; $i++ )
{
$foo[$i] = $i;
}
echo 'Created big array!'."\n";
sleep( 10 );
//echo 'Mem Usage: '.memory_get_usage()."\n";
$numEntries = count( $foo );
echo 'Counted big array!'."\n";
sleep( 10 );
//echo 'Mem Usage: '.memory_get_usage()."\n";
?>
Using the following command:
watch -n 0 'ps awxu | grep foo.php | grep -v grep'
I got the following snapshots during the two sleep steps:
rob 16018 66.7 44.7 935084 928684 pts/7 S+ 17:11
0:18 /usr/local/bin/php -qC ./foo.php
rob 16018 43.9 44.7 935084 928684 pts/7 S+ 17:11
0:18 /usr/local/bin/php -qC ./foo.php
Which indicated no change from the 935 megs of memory already allocated
before the count().
You've either encountered a bug in your version, or a confounding
variable :)
Cheers,
Rob.
--
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php