Code instance sharing for write-time copy (copy on write) in PHP

Source: Internet
Author: User

Problem Introduction

First look at the assignment and reference problems in PHP

<?php$a = 10;//assigns a constant value to a variable, allocates a memory space for a $b = $a;//variable is assigned to the variable, is not copy a copy, B also allocated memory space? $c = & $a;//references do not allocate space for C, and C and a are shared with one part of the space.?>

What is your answer to the question in the middle? Before today, my answer is to allocate memory space for B. Because that's what I understand:
& when assigning a value, consider a variable to define an alias, and add a reference to the memory space. Changing one of these will affect the other references. When using unset (), only a reference to the variable memory space is broken, and the memory space is not freed.
and = Assignment is different, it will re-open a memory space to store a copy of the original variable. The changes between the two do not affect each other.

This is confirmed by the following procedure:

<?php$a = ten;   Assigning a constant value to a variable allocates a memory space for a $b = $a; Variable assigned to the variable, is not copy a copy, B also allocated memory space? $c = & $a; References do not allocate space for C, and C and a are shared in a single piece of space. $a = 5;echo $c;   Output 5, because A and C are pointing to the same memory space Echo Php_eol;echo $b;   Since B is a copy, the operation on a does not affect B, the output 10?>

What if

$b = $a;//After a  and  b do not make any changes, remain consistent

There is such a problem, if = after the assignment, two variables have not changed, if it is two copies, is not too wasteful memory?
This is actually avoided in PHP.
When you assign a variable to a new variable in PHP, the new variable is not immediately allocated a memory space, but a reference to the memory space is added. A new variable is allocated a memory space when the original variable or new variable makes any changes.

<?php$a = 1; $b = $a; echo $a;//Before this, B is a shared memory space with a. $a = 2;//a made a change, at which point B will have its own space?>

Each PHP variable exists in a variable container called "Zval". A Zval variable container that includes two bytes of extra information in addition to the type and value of the variable. The first is "Is_ref", which is a bool value that identifies whether the variable belongs to a reference collection (Referenceset). With this byte, the PHP engine can differentiate between normal and reference variables, and since PHP allows users to use the custom reference by using &, there is an internal reference counting mechanism in the Zval variable container to optimize memory usage. The second extra byte is "RefCount", which represents the number of variables (also known as symbols) that point to the Zval variable container. When the value of "RefCount" is 1 o'clock, the value of "Is_ref" is always false.

After installing Xdebug, using Xdebug_debug_zval (), you can see the ZVAL structure:
As follows:

<?php$a = 1; $b = $a; echo $a;//Before this, B is a shared memory space with a. Xdebug_debug_zval (' B '); $a = 2;//a made a change, at which point B will have its own space xdebug_debug_zval (' B '); >

Output:

B:   (refcount=2, is_ref=0),   int    1b:   (Refcount=1, is_ref=0),   int    1

As can be seen from the above results, the reference count is 2 before a change, and when a is changed, the reference count of B becomes 1 because B re-allocates space.
The phenomenon described above is copy-on-write.

Copy when writing

  copy -on-write (copy-on-write, also abbreviated as Cow), as the name implies, is only true when writing a copy of the memory to modify. Cow is the earliest application in the *nix system for threading and Memory usage optimization, which is widely used in various programming languages, such as C + + STL. In the PHP kernel, cow is also the main memory optimization tool. In the previous discussion of variables and memory, reference counting plays a key role in the destruction and recycling of variables. Reference counting exists in order to make the cow function properly, so that the memory can be optimized for use.
   Copy Advantage : When assigning a value to a variable by assignment, it does not request new memory to hold the value saved by the new variable, but simply uses a counter to share the memory. A new space is requested to hold the value content to reduce memory consumption only if the value of one of the references to the variable changes.

  From the underlying data structure of PHP

Ref_count and is_ref are defined in the ZVAL structure;
Is_ref identifies whether the user uses & 's mandatory reference;
Ref_count is a reference count that identifies how many variable references to this zval, that is, the automatic reference to copy on write, which is destroyed when it is 0.

Problem Introduction

First look at the assignment and reference problems in PHP

<?php$a = 10;//assigns a constant value to a variable, allocates a memory space for a $b = $a;//variable is assigned to the variable, is not copy a copy, B also allocated memory space? $c = & $a;//references do not allocate space for C, and C and a are shared with one part of the space.?>

What is your answer to the question in the middle? Before today, my answer is to allocate memory space for B. Because that's what I understand:
& when assigning a value, consider a variable to define an alias, and add a reference to the memory space. Changing one of these will affect the other references. When using unset (), only a reference to the variable memory space is broken, and the memory space is not freed.
and = Assignment is different, it will re-open a memory space to store a copy of the original variable. The changes between the two do not affect each other.

This is confirmed by the following procedure:

<?php$a = ten;   Assigning a constant value to a variable allocates a memory space for a $b = $a; Variable assigned to the variable, is not copy a copy, B also allocated memory space? $c = & $a; References do not allocate space for C, and C and a are shared in a single piece of space. $a = 5;echo $c;   Output 5, because A and C are pointing to the same memory space Echo Php_eol;echo $b;   Since B is a copy, the operation on a does not affect B, the output 10?>

What if

$b = $a;//After a  and  b do not make any changes, remain consistent

There is such a problem, if = after the assignment, two variables have not changed, if it is two copies, is not too wasteful memory?
This is actually avoided in PHP.
When you assign a variable to a new variable in PHP, the new variable is not immediately allocated a memory space, but a reference to the memory space is added. A new variable is allocated a memory space when the original variable or new variable makes any changes.

<?php$a = 1; $b = $a; echo $a;//Before this, B is a shared memory space with a. $a = 2;//a made a change, at which point B will have its own space?>

Each PHP variable exists in a variable container called "Zval". A Zval variable container that includes two bytes of extra information in addition to the type and value of the variable. The first is "Is_ref", which is a bool value that identifies whether the variable belongs to a reference collection (Referenceset). With this byte, the PHP engine can differentiate between normal and reference variables, and since PHP allows users to use the custom reference by using &, there is an internal reference counting mechanism in the Zval variable container to optimize memory usage. The second extra byte is "RefCount", which represents the number of variables (also known as symbols) that point to the Zval variable container. When the value of "RefCount" is 1 o'clock, the value of "Is_ref" is always false.

After installing Xdebug, using Xdebug_debug_zval (), you can see the ZVAL structure:
As follows:

<?php$a = 1; $b = $a; echo $a;//Before this, B is a shared memory space with a. Xdebug_debug_zval (' B '); $a = 2;//a made a change, at which point B will have its own space xdebug_debug_zval (' B '); >

Output:

B:   (refcount=2, is_ref=0),   int    1b:   (Refcount=1, is_ref=0),   int    1

As can be seen from the above results, the reference count is 2 before a change, and when a is changed, the reference count of B becomes 1 because B re-allocates space.
The phenomenon described above is copy-on-write.

Copy when writing

  copy -on-write (copy-on-write, also abbreviated as Cow), as the name implies, is only true when writing a copy of the memory to modify. Cow is the earliest application in the *nix system for threading and Memory usage optimization, which is widely used in various programming languages, such as C + + STL. In the PHP kernel, cow is also the main memory optimization tool. In the previous discussion of variables and memory, reference counting plays a key role in the destruction and recycling of variables. Reference counting exists in order to make the cow function properly, so that the memory can be optimized for use.
   Copy Advantage : When assigning a value to a variable by assignment, it does not request new memory to hold the value saved by the new variable, but simply uses a counter to share the memory. A new space is requested to hold the value content to reduce memory consumption only if the value of one of the references to the variable changes.

  From the underlying data structure of PHP

Ref_count and is_ref are defined in the ZVAL structure;
Is_ref identifies whether the user uses & 's mandatory reference;
Ref_count is a reference count that identifies how many variable references to this zval, that is, the automatic reference to copy on write, which is destroyed when it is 0.

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.