Php garbage collection mechanism-copy during write and reference count

Source: Internet
Author: User
: This article mainly introduces the php garbage collection mechanism-copy during writing and reference counting. For more information about PHP tutorials, see. PHP uses reference count and write-time replication to manage memory. During writing, replication ensures that the replication value between variables does not waste memory, and the reference count ensures that the memory is released to the operating system when the variables are no longer needed.

To understand PHP memory management, you must first understand a concept-symbol table.

Symbol table concept:

A variable consists of two parts: the variable name and the variable value. The symbol table maps the variable name to the array of the address where the variable value is located in the memory.

Copy at Write Time:

When the value of a variable is copied to another variable, PHP does not use more memory for the copied value. Instead, it will show the new symbol table that these two variables have the same memory block. Therefore, the following code does not create a new array:

      

$ People = array ("Alice", "Bob ");

$ Other = $ people; // The array is not copied.

?>

If you modify any copy, PHP allocates the required memory for replication:

$ People [1] = "Tom"; // The value is changed. the array is copied and new memory is allocated.

Through delayed allocation and replication, PHP saves time and memory in many cases, which is replication during writing.

Reference count:

No job in the symbol table has a reference counter. its value indicates the number of methods for obtaining the memory.

After $ people and $ other are initialized and assigned values, the array points to the symbol table. the values of $ people and $ other reference counters are 2. in other words, there are two ways to get the memory: $ people and $ other.

However, when $ people [1] is changed, PHP creates a new array for $ people. at this time, the reference counter values of $ people and $ other are both 1. when a variable leaves the scope, for example, when the function parameters and local variables reach the end of the function, the value of the application counter minus 1. when a variable is assigned a value in other memory spaces, the old reference count is reduced by 1. When the reference count value is 0, its memory is released.

This is the reference count.

Reference count is the preferred memory management method. Keep the scope of variables limited to functions, pass through values, and let the reference count be responsible for memory management. If you want to actively obtain more information or control to release the value of a variable, you can use the isset () and unset () functions.

The above introduces the php garbage collection mechanism-copy at write time and reference count, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.