How to Manage Memory in PHP | 3
How to Manage Memory in PHP
Reference Counting
Careful memory allocation and freeing is vital to the long term performance of a multirequest process like PHP, but it's only half the picture. In order for a server that handles thousands of hits per second to function efficiently, each request needs to use as little memory as possible and perform the bare minimum amount of unnecessary data copying. Consider the following PHP code snippet:
After the first call, a single variable has been created, and a 12 byte block of memory has
been assigned to it holding the string ÂHello World' along with a trailing NULL. Now
look at the next two lines: $b
is set to the same value as $a
, and then $a
is unset (freed).
If PHP treated every variable assignment as a reason to copy variable contents, an
extra 12 bytes would need to be copied for the duplicated string and additional
processor load would be consumed during the data copy. This action starts to look
ridiculous when the third line has come along and the original variable is unset making
the duplication of data completely unnecessary. Now take that one further and imagine
what could happen when the contents of a 10MB file are loaded into two variables. That
could take up 20MB where 10 would have been sufficient. Would the engine waste so
much time and memory on such a useless endeavor?
You know PHP is smarter than that.
Remember that variable names and their values are actually two different concepts
within the engine. The value itself is a nameless zval*
holding, in this case, a string
value. It was assigned to the variable $a
using zend _hash_add()
. What if two variable
names could point to the same value?
At this point you could actually inspect either $a
or $b
and see that they both contain
the string "Hello World"
. Unfortunately, you then come to the third line: unset($a);
.
In this situation, unset()
doesn't know that the data pointed to by the $a
variable is also
in use by another one so it just frees the memory blindly. Any subsequent accesses to $b
will be looking at already freed memory space and cause the engine to crash. Hint:You
don't want to crash the engine.
This is solved by the third of a zval's four members: refcount
. When a variable is first
created and set, its refcount
is initialized to 1 because it's assumed to only be in use by
the variable it is being created for. When your code snippet gets around to assigning
helloval
to $b
, it needs to increase that refcount
to 2 because the value is now "referenced"
by two variables:
Now when unset()
deletes the $a
copy of the variable, it can see from the refcount
parameter that someone else is interested in that data and it should actually just
decrement the refcount and otherwise leave it alone.
Copy on Write
Saving memory through refcounting is a great idea, but what happens when you only want to change one of those variables? Consider this code snippet:
Looking at the logic flow you would of course expect $a
to still equal 1, and $b
to now
be 6. At this point you also know that Zend is doing its best to save memory by having
$a
and $b
refer to the same zval
after the second line, so what happens when the third
line is reached and $b
must be changed?
The answer is that Zend looks at refcount
, sees that it's greater than one and
separates
it. Separation in the Zend engine is the process of destroying a reference pair
and is the opposite of the process you just saw:
Now that the engine has a zval*
that it knows is only owned by the $b
variable, it can
convert it to a long and increment it by 5 according to the script's request.
Created: March 27, 2003
Revised: June 19, 2006
URL: https://webreference.com/programming/php_mem/1