I'm trying to figure out how does PHP load arrays to memory and when does passing an array consume memory.
So I’ve got this little bit of code running: note that the input array is less important in this example:
<?php
echo $this->getMemoryUsage();
$arr = $query->result_array(); // array of arrays from codeigniter
echo $this->getMemoryUsage();
This consumes exactly 250 kB of memory, this means the array is roughly 250 kB in size, roughly.
So I ran the following code:
<?php
echo $this->getMemoryUsage();
$arr = $query->result_array(); // array of arrays from codeigniter
$arr[0]['id'] = 'changing this value';
$foo = $arr;
$foo[2]['id'] = 'changing this value again';
$bar = $foo;
$bar[4]['id'] = 'changing this value again and again';
$far = $bar;
$far[5]['id'] = 'changing this value again and again and again';
echo $this->getMemoryUsage();
According to what I read and was told, PHP doesn’t actually copy the array, it only references the original array, but once a change is made PHP has to copy the entire array.
Imagine my surprise when the above code consumes exactly 500 kB of RAM.
Can anyone explain what’s going on here?
Just to be clear, all these indices (0–5 and id) already exist in the original array, I’m just modifying the value. The original value is some integer.
EDIT
Just to clear the involvement of $this->result(); Here's another test I've conducted :
echo $this->getMemoryUsage();
$arr = $query->result_array(); // array of arrays from codeigniter
//$arr[0]['id'] = 'changing this value';
$foo = $arr;
$foo[2]['id'] = 'changing this value again';
//$bar = $foo;
//$bar[4]['id'] = 'changing this value again and again';
//
//$far = $bar;
//$far[4]['id'] = 'changing this value again and again and again';
echo $this->getMemoryUsage();
This time the output is exactly 250 kB - Just like the original trial without any changes
EDIT #2
As requested, I've ran the code from here on my setup, to make sure results are consistent : http://pastebin.com/cYNg4cg7
These are the results :
DECLARATION: 4608 kB
FINAL: 8904 kB
DIFF TO DECLARATION: 4296 kB
So even though the declaration was 4608 and the array was passed and changed 4 times, it's still only less than doubled the memory footprint.
EDIT #3
I've ran the memory changes after each allocation :
DECLARATION: 5144 kB
allocating A0 added : 144 kB
allocating A1 added : 1768 kB
allocating A2 added : 1768 kB
allocating A3 added : 1768 kB
FINAL: 10744 kB
DIFF TO DECLARATION: 5600 kB
Each following operation after the first costs exactly the same, which seems to indicate the exact same size is being copied. This seems to support Austin's answer, The only thing that doesn't add up now is the size that's allocated, But that's a different question .
Seems like Austin's on the ball, I'll accept it if no other answer comes by.
result_array()method, and is not (yet) cleaned up). To be sure, I would make a new copy of the array and measure the difference before and after that copy, although that will also not be completely waterproof.