2

In case I have this code

<?php
$array = array();
for ($i=1;$i<100000;$i++){
    $array[$i] = md5(rand(0,9999999999999999));
}

$array2 = $array;

$array takes about 0.5MB RAM, let's say. Does PHP proccess take about 1.0MB RAM with $array2 = $array; ? and in this case

<?php
class rand{
    public $array;
    function rand(){
        $this->array = array();
        for ($i=1;$i<100000;$i++){
            $this->array[$i] = md5(rand(0,9999999999999999));
        }

    }
}

$class = new rand();
$class2 = $class;

$class takes about 0.5MB RAM, let's say . Does PHP proccess take 1.0MB with $class2 = $class?

is it same?

Tests:

genesis
  • 50,477
  • 20
  • 96
  • 125
  • Check it out for yourself. See get_memory_usage() – John Cartwright Jul 28 '11 at 17:44
  • @JohnCartwright: Wow. It looks it's not copying themselves. Look [first](http://sandbox.phpcode.eu/g/62669.php), [second](http://sandbox.phpcode.eu/g/aee4d.php) – genesis Jul 28 '11 at 17:48
  • @genesis: You need to write another get_memory_usage() for the first variable, array or object. Your code doesn't do anything. – Micromega Jul 28 '11 at 17:53
  • Oops, I meant memory_get_usage() – John Cartwright Jul 28 '11 at 17:54
  • @Jitamaro: see my question edited. It is testing exact code I have in question – genesis Jul 28 '11 at 17:57
  • I assume this is a caching strategy, try this **before** the second memory_get_usage(): array_pop($array2); the memory increases rapidly, why? because php caches the copy of the array as a reference as long as you do not modify it. If you start modifying it, the array gets copied in the memory. This won't happen with the class. – sled Jul 28 '11 at 18:04

1 Answers1

1

This is what the PHP manual in the reference section warns about: the Engine is smart enough. Setting the $array2 = $array; does not cause duplicate storage, as PHP recognizes they are still both the same. However, try a $array[2] = 'something;' after that. PHP detects the difference, and only then will copy the values.

<?php
$array = array();
for ($i=1;$i<100000;$i++){
    $array[$i] = md5(rand(0,9999999999999999));
}
echo memory_get_usage().PHP_EOL;
$array2 = $array;
echo memory_get_usage().PHP_EOL;
$array['foo'] = 'bar';
echo memory_get_usage().PHP_EOL;
//17252052
//17252156
//23776652

Classes are references by default, and only a clone $object would result in 2 objects in PHP >= 5.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • How is it possible that it [takes a lot more](http://sandbox.phpcode.eu/g/d6771.php) memory for me? extensions? – genesis Jul 28 '11 at 18:34
  • is there an option to kill this smart behaviour? I can compile myself! – Micromega Jul 28 '11 at 18:37
  • @genesis: edited the classes answer in. And why it takes more? There's a random factor of course, but this is from a lean & mean command-line php script, not in a webserver, which is quite a difference. – Wrikken Jul 28 '11 at 18:45
  • @Jitamaro: the C source code of PHP is available, go ahead. I can't for the life of me think _why_ you would destroy this behavior though. There are as of yet no ill sideeffects of this behavior that I know of. – Wrikken Jul 28 '11 at 18:46
  • @genesis: you see you need 3 memory_get_usage!!! I want an upvote now for my post or I delete it!!! – Micromega Jul 28 '11 at 18:47
  • @Jitamaro: just go ahead deleting. this is better answer. sorry – genesis Jul 28 '11 at 18:51
  • [puts on camp voice] Oh,behave! [/back to normal] – Wrikken Jul 28 '11 at 19:01
  • I told him that before he don't believe and he is voting you. It's logic. – Micromega Jul 28 '11 at 19:13