Garbage collection mechanism in PHP

Source: Internet
Author: User

PHP uses Copy-on write and reference count to manage memory.

Copy-on-write is also abbreviated as COW (Copy upon write). It is an optimization strategy in computer programming. Copy-on-write in PHP, you can think that multiple variables use the same Copy of information, that is, these variables all point to the same memory address. Because only these variables are read. There is no need to copy the same value for each variable in the memory, which saves a lot of memory resources. But when a variable needs to be modified, copy the real object to the new memory address, and modify the memory ing table of the new object to point to this new location, and execute the write operation on the new memory location.

The following code:

<? Php $ a = array (, 5); $ B = $ a; // The array is not copied $ a [1] = 10; // The array is copied, the value print_r ($ a); print_r ($ B);?>

After running, the values of $ a and $ B are different.

$ A is 1, 10, 5

$ B is 1, 4, 5

This is a bit similar to the value assignment of the C # value type. To make $ a and $ B always be the same reference, write the code as follows:

$b=&$a;

A term used in PHP to match the Copy-on-write technology is reference count ).

In PHP, each variable is composed of two parts: a variable name and a variable value. They are stored in a structure called a symbol table, which is an array, it maps the location of the variable name and value in the memory. Each value in the symbol table has a so-called reference count, which records how many methods can be used to obtain this value, that is, how many variable names point to this value.

In the code above, after $ a is initialized and $ B = $ a, this array has a reference count 2. If you view the reference count through the C API method, this value is actually 3, but from the user's point of view, it can be better understood as 2 ). That is to say, the value in the memory can be obtained in two ways, through $ a and $ B. then, when the value of $ a [1] is changed, php creates a new memory space for $ a, that is, two arrays. The reference count of both arrays is 1. When a variable out of the scope, such as a local variable in the function, the variable becomes invalid after the function is run, the reference count of the value pointed to by this variable is reduced by 1. Similarly, if a variable points to a new memory address, the reference count on the value of this old address will be reduced by 1. When the reference count of a memory space is 0, it will be released by PHP.

This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/1281534

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.