Memory Management for PHP variables

Source: Internet
Author: User
Tags zend
This article mainly introduces the PHP variable memory management, has a certain reference value, now share to everyone, the need for friends can refer to

Each computer language requires containers to hold variable data. In some languages, variables have specific types, such as strings, arrays, objects, and so on. For example, C and Pascal belong to this. PHP does not have such a type. In PHP, a variable in a row is a string, and the next line may become a number. Variables can often be easily converted between different types, or even automatically. PHP has become a simple and powerful language, and a large part of it is because it has weak types of variables. But sometimes it brings up some interesting questions.

Inside PHP, variables are stored in a container called Zval. It contains not only the value of the variable, but also the type of the variable. Python is similar to PHP and has a tag tag variable type. The variable container contains some fields that the Zend engine uses to differentiate whether or not to reference. It also contains a reference count for this value.

Variables are stored in a symbol table equivalent to an associative array. This array takes the variable name key and points to the container that contains the variables. As shown in the following:


Reference count

PHP tries to be smarter when copying variables (such as $a = $b). "=" is also called an assignment operator. When an assignment is performed, the Zend engine does not create a new variable window, but instead increases the refcount field of the variable window, and you can imagine how much memory this will save when the variable is a huge string or a huge array. As shown in the following:


The first step: variable A, which contains the text "This is". By default, the reference count equals 1

Step Two: Assign the variable $ A to $b and $c. There is no new variable container generation, just refcount plus 1 for each variable assignment operation. Since the assignment was performed two times, the RefCount will eventually become 3.

Now, maybe you'd like to know what happens when the variable $c changes. Depending on the value of the refcount, it can be handled in two different ways. If RefCount equals 1, the variable container will update its value (perhaps updating its type at the same time). If RefCount is greater than 1, a variable container with the new value (and type) is created. In the third step shown in 2, the RefCount value of the variable container where the $a variable is located is subtracted by one, and now the value of RefCount is 2, and the value of the newly created container refcount is 1. When using the unset function on a variable, the RefCount value of the container in which the variable resides is subtracted by one, as shown in the 4th step. If the value of RefCount is less than the 1,zend engine will translate this variable container, the 5th step is shown.

Passing variables to functions (passing Variables to Functions)

In addition to the global symbol tables that are common to all scripts, each user-defined function creates a symbol table of its own when called, to hold its own variables. When a function is called, the Zend engine creates a symbol table that will be freed when the function returns. A function is either returned by a return statement or returned by the end of the function (the translator Note: a function with no return returns null by default). As shown in the following:


Figure 3 Details how the variable is passed to the function.

In the first step, we assign "Thisis" to the variable $ A, and then we pass the variable to the $s variable of the do_something () function.

In the second step, you can see that this is the same as the variable assignment (similar to the $b = $a we mentioned in the previous section), except that it is stored in a different symbol table (the function symbol table), and the reference count is added 2 instead of the plus 1. The reason is that the function stack also contains a reference to this variable container.

In the third step, when we assign a new value to the variable $s, the refcount of the original variable is reduced by 1, and a variable container with the new value is created.

In the fourth step, we return a variable using the return statement. The returned variable gets an entity from the global symbol table and increments its refcount value by 1. When the function ends, the symbol table of the function is destroyed. During the destruction process, the Zend engine traverses each variable in the symbol table and reduces its refcount value. When the value of the refount of the variable container becomes 0, the variable container will be destroyed. As you can see, because of the reference counting mechanism of PHP, the variable container is not returned from the function as a copy. If the variable $s is not modified in the third step, the variable $ A and $b will always point to the same variable container (the container's refcount is 2). In this case, the statement $ A = "This is" will not create a copy of the variable container.

Related recommendations:

PHP variable write-time replication mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.