Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does referencing a variable in a php function help save memory?

Tags:

For example are the following two functions the same or not when one compares how memory is being managed:

$hello = 'hello';

my_echo($hello);

//Better or worse than
my_echo_ref($hello);

//case 1, no referencing:
function my_echo($word) {
 echo $word;
} 

//case 2, referencing:
function my_echo_ref(&$word) {
  echo $word;
}
like image 829
dkinzer Avatar asked Jun 27 '10 01:06

dkinzer


People also ask

What is the use of reference variable in PHP?

A PHP reference is an alias, which allows two different variables to write to the same value. In PHP, an object variable doesn't contain the object itself as value. It only contains an object identifier which allows object accessors to find the actual object.

Should you pass by reference in PHP?

The short answer is use references when you need the functionality that they provide. Don't think of them in terms of memory usage or speed. Pass by reference is always going to be slower if the variable is read only. Everything is passed by value, including objects.

Is passing by reference faster PHP?

The bigger the array (or the greater the count of calls) the bigger the difference. So in this case, calling by reference is faster because the value is changed inside the function.

How does PHP manage memory?

PHP memory management functions are invoked by the MySQL Native Driver through a lightweight wrapper. Among others, the wrapper makes debugging easier. The various MySQL Server and the various client APIs differentiate between buffered and unbuffered result sets.


2 Answers

I don't know how reliable this source is, but this is a very interesting article (from 2008) that explains how variables are handled:

The Truth About PHP Variables

It says

I wanted to write this post to clear up what seems to be a common misunderstanding in PHP – that using references when passing around large variables is a good way save memory.

and

(...) While the above explanation of references is sufficient for a general understanding, it is often useful to understand how PHP handles variable assignment internally. This is where we introduce the concept of the zval.

zvals are an internal PHP structure which are used for storing variables. Each zval contains various pieces of information, and the ones we will be focusing on here are as follows:

  • The actual data stored within the zval – In our example this would be either ‘hello!’ or ‘goodbye!’
  • is_ref Boolean flag
  • A ref_count counter

(...)

and

(...) When you assign a variable by value (such as in example 1) it does not create a new zval, it simply points both variables at the same zval and increases that zval’s ref_count by one. “Wait!” I hear you cry, “Isn’t that passing by reference?” Well, although it sounds the same, all PHP is doing is postponing any copying until it really has to – and it knows this because is_ref is still false. (...)

and the conclusion:

You can see that, unless the developer is completely consistent, passing variables by reference can easily lead to increased memory usage.


Besides that, I ran your code a few times with get_memory_usage() and there was no difference in memory consumption (but this does not necessarily mean anything, probably the memory consumption differs when one is actually doing something with the variable).

like image 127
Felix Kling Avatar answered Sep 19 '22 09:09

Felix Kling


For the record (php 7.0 - win32)

I am testing it (i passed a variable to a function then i changed the value inside the function) and i found:

a) unset($var) and $var=null act equally.

b) passing an array by reference versus by value, doubles the memory usage at some point. Why?, i don't know. I found it with memory_get_peak_usage(). However, memory_get_usage() will not change so i guess that its duplicates the information (may be in a temporal variable) then it discards.

For example: (the memory is just an approximation)

  $var= (10mb of data as an array)  = memory peak 10mb.
  function functionval($var) {
       $var= (100mb of data) = memory peak 110mb.
  }
  function functionref(&$var) {
       $var= (100mb of data) = memory peak 210mb. !!! 
  }

but

  $var= (100mb of data as an array)  = memory peak 100mb.
  function functionval($var) {
       $var= (10mb of data) = memory peak 110mb.
  }
  function functionref(&$var) {
       $var= (10mb of data) = memory peak 120mb. !!! 
  }

and

  $var= (100mb of data as an array)  = memory peak 100mb.
  function functionval($var) {
       $var= (100mb of data) = memory peak 200mb.
  }
  function functionref(&$var) {
       $var= (100mb of data) = memory peak 300mb. !!! <-- why???
  }

also

  $var= (100mb of data as an array)  = memory peak 100mb.
  function functionval($var) {
       not changing the data. = memory peak 100mb.
  }
  function functionref(&$var) {
       not changing the data. = memory peak 100mb.
  }

c) passing parameters as value or reference will practically not change the execution time.

d) passing objects as reference, doubles the memory usage at some point.

like image 34
magallanes Avatar answered Sep 19 '22 09:09

magallanes