Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9695

Re: Exhaustive memory allocation using arrays

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: Exhaustive memory allocation using arrays
Date 2013-05-29 08:22 +0000
Organization A noiseless patient Spider
Message-ID <ko4dt0$566$1@dont-email.me> (permalink)
References <c85ab849-eed2-4dce-8d22-074867a93ab5@googlegroups.com>

Show all headers | View raw


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@gmail.com

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Exhaustive memory allocation using arrays tombert.at@gmail.com - 2013-05-28 23:27 -0700
  Re: Exhaustive memory allocation using arrays tombert.at@gmail.com - 2013-05-28 23:42 -0700
  Re: Exhaustive memory allocation using arrays tombert.at@gmail.com - 2013-05-29 00:45 -0700
    Re: Exhaustive memory allocation using arrays Christoph Becker <cmbecker69@gmx.de> - 2013-05-29 13:00 +0200
  Re: Exhaustive memory allocation using arrays Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-29 08:22 +0000
    Re: Exhaustive memory allocation using arrays The Natural Philosopher <tnp@invalid.invalid> - 2013-05-29 09:54 +0100
  Re: Exhaustive memory allocation using arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-29 15:30 +0200

csiph-web