PHP reference count and copy-to-write tutorial

Source: Internet
Author: User
Understand PHP reference count and write-time replication. Php uses copy-on-write to manage the memory. Reference computation ensures that the memory is returned to the operating system when the reference is no longer needed. During writing, copy-on-write ensures that the reference count and copy-on-write are used in the php repeat between variables) to manage the memory. The reference calculation ensures that the memory is returned to the operating system when the reference is no longer needed, and the replication during writing ensures that the memory is not wasted when the value is copied between variables.

To understand the memory management in PHP, you must first understand the idea of the symbol table. the variable has two parts: the variable name (such as $ name) and variable values (such as "Fred "). A symbol table is an array that maps variable names to locations in memory.

When copying a value from one variable to another, PHP does not get more memory because of copying the value, but updates the symbol table, to indicate that "the two variables are the same memory name ". The following code does not actually create a new array:

$ People = array ("Gonn", 25, "Zeng"); $ programmer = $ people; // The array is not copied

If any copy is modified, PHP allocates memory and generates the copy:

$ People [1] = 26; // The array is copied and the value changes.

Due to delayed allocation and replication, PHP saves time and memory in many cases. This is the replication during writing.

Each value pointed to by the symbol table has a reference count, which is a number that represents the number of paths to the memory. After assigning the initial values of the array to $ people and $ people to $ programmer, the entry pointing to the array in the symbol table is $ people and $ programmer, and the reference count is 2. In other words, there are two ways to reach that piece of memory: through $ people or $ programmer. But after $ people [1] is changed, PHP creates a new array for $ people, and the reference count of each array is only 1.

When a variable is not in the scope (function parameters or local variables are at the end of the function), the reference count value is reduced by 1. When the assigned value of a variable is in other areas of the memory, the old reference count value is reduced by 1. When the reference count reaches 0, the memory is released. This is the reference count.

The preferred way to manage memory by referencing a counter is to keep the variable's function to locally pass the value required by the function, and let the reference count be responsible for releasing the memory when the reference is no longer needed. If you want to obtain more information or fully control the release variable value, you can use the isset () and unset () functions ().

To check whether the variable has been set (even a null string), use isset ():

$ S1 = isset ($ name); // $ s1 is false $ name = "Gonn"; $ s2 = isset ($ name); // $ s2 is true

Use unset () to delete the value of a variable:

$ Name = "Gonn"; unset ($ name); // $ name is NULL

Additional reading: http://php.net/manual/zh/features.gc.refcounting-basics.php

Memory (copy-on-write) to manage the memory. The reference calculation ensures that the memory is returned to the operating system when the reference is no longer needed, and the replication during writing ensures that the memory is repeat between variables...

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.