2

Because $a='a' allocates one byte but $b is not modified.

In PHP, for the code "$a='a';$b=$a;" allocates 2 units of memory or 1 unit? how can I test it?

I can say that pseudoly like

alphahandler = new stringObj
alphahandler->insertContent('a');
variable a = alphahandler.objectHandler();

comes to b

variable b = a.getCurrentObjectHandler() 

or just like

b=a.getCurrentObject().toContent()

??

Abby Chau Yu Hoi
  • 1,378
  • 3
  • 15
  • 37
  • 3
    You really shouldn't be worrying about this. Even in languages where you should care about memory use the use of a single variable with a scalar value is never going to be a source of significant memory. – Billy ONeal Dec 12 '11 at 02:48
  • 1
    As a practical matter, `$a = 'a'` would definitely allocate more than one byte. PHP is not C. – Jon Dec 12 '11 at 02:50
  • 2
    Also more like 400 bytes. The actual memory overhead comes from entries in the hash of the local variable scope. – mario Dec 12 '11 at 02:52
  • um... I actually want to mean the comparison. I have changed the question due to that, thanks. I am not worrying about taking much memory or not but I want the concept for further development. – Abby Chau Yu Hoi Dec 12 '11 at 03:04

3 Answers3

3

Regarding testing, you can use memory_get_usage(); for an approximation.

See also:

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • 2
    Not really, because if he is testing such small quantities, then as soon as he uses `memory_get_usage`, he uses more memory than the quantity being tested. – Petah Dec 12 '11 at 02:56
2

How many times is memory allocated?

$a='a';   # creates a new string value, and assigns it to $a
$b=$a;    # assigns the SAME string value -- the one now in $a -- to $b

Ignoring that both variables also take up memory (and require separate variable entries), there is only one string object created from the above code.

That is, both variables now refer to the same value: there was no "copy" of the string created upon assignment.

How much memory is allocated?

There is much more memory being allocated (than say, 1 or 2 bytes): there is the memory for the object and then there is the memory for each variable.

While this question relates to integers, similar stuff happens for strings (actually, more happens). A simple integer takes at least 72 bytes of memory. As mario suggested in a comment, this example may require upwards of 400 bytes.

Happy coding.

Community
  • 1
  • 1
0

Short answer is a link to another answer on so, but I think it'll prove useful as this is a complicated matter. what seems logical due to the way php works may not actually be how the core implementation will manage the memory.

[edit] A simple script to illustrate the internals.

Strings are not objects in PHP - objects act totally differently than scalar variables. If you're asking about string variables types the following script should shed some light on the internals.

<?php

header( 'content-type: text/plain' );
$a = 'a';
$b = $a;

echo '$b: ';  debug_zval_dump( $b );
$b = 'b';
echo '$a: '; debug_zval_dump( $a );
echo '$a: '; debug_zval_dump( $b );

Output

$b: string(1) "a" refcount(3)
$a: string(1) "a" refcount(2)
$a: string(1) "b" refcount(2)

[edit] - another illustration with memory usage

<?php

header( 'content-type: text/plain' );
$start = memory_get_usage();
echo 'Mem: ' . ( memory_get_usage() - $start ) . PHP_EOL; $start = memory_get_usage();
$a = 'a';
echo 'Mem: ' . ( memory_get_usage() - $start ) . PHP_EOL; $start = memory_get_usage();
$b = $a;
echo 'Mem: ' . ( memory_get_usage() - $start ) . PHP_EOL; $start = memory_get_usage();


echo '$b: ';  debug_zval_dump( $b );
$b = 'b';
echo 'Mem: ' . ( memory_get_usage() - $start ) . PHP_EOL; $start = memory_get_usage();
echo '$a: '; debug_zval_dump( $a );
echo 'Mem: ' . ( memory_get_usage() - $start ) . PHP_EOL; $start = memory_get_usage();
echo '$a: '; debug_zval_dump( $b );
echo 'Mem: ' . ( memory_get_usage() - $start ) . PHP_EOL; $start = memory_get_usage();

Output

Mem: 76
Mem: 88
Mem: 48
$b: string(1) "a" refcount(3)
Mem: 40
$a: string(1) "a" refcount(2)
Mem: 0
$a: string(1) "b" refcount(2)
Mem: 0
Community
  • 1
  • 1
Tim G
  • 1,812
  • 12
  • 25
  • The refcount is higher than you'd expect because calling debug_zval_dump creates a copy of the var. – Tim G Dec 12 '11 at 05:03