FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Exhaustive memory allocation using arrays
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Exhaustive memory allocation using arrays [message #181747 is a reply to message #181744] Wed, 29 May 2013 08:22 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Tue, 28 May 2013 23:27:13 -0700, tombert.at wrote:

> Why is the following code using > 100MB?
>
> for ($i=0; $i < 1000000; $i++) {
> $value[$i] = 23.4567890123;
> }
> echo (memory_get_usage(TRUE) / (1024 * 1024)) . "MB <br>";
>
> It looks like that each item takes 100 Byte for allocation??

From: http://php.net/manual/en/language.types.array.php

"An array in PHP is actually an ordered map."

You may be expecting arrays where an array of 1e6 floats is held in
memory in 1e6 sequential 8 byte locations, for a total of 8e6 bytes, but
I suspect that if you did, you'd be very very very wrong.

It's possible that:

$arr = array_values( $arr );

May shrink your array, I don't know for sure.

It's also possible that:

$a = Array();
for ( $i = 0; i < 1e6; i++ )
$a[] = 23.4567890123;

might produce a smaller array, simply because it's not assigning keys.
Again, I don't know for sure.

The reason I don't know for sure is that these implementation details
are, to me, just that. If they don't affect me, I don't need to
understand them in the level of detail you're looking for. There's plenty
of things I do need to understand at that level of detail, and I have
limited time to put into gaining such understandings, so I use it where
it's most needed. :)

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Parsing mbox files with Windows Php
Next Topic: UML
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 29 23:30:32 GMT 2024

Total time taken to generate the page: 0.04812 seconds