Home > Article > Backend Development > What does Copy on write mean in PHP?
Copy on write in PHP means that when assigning values to variables using variables, these variables with the same value point to the same memory, only when these point to the same value of the same memory, and one of the variables Variable separation is only required when the value of a variable changes.
Copy on write in PHP means:
Copy on write
(Copy -on-Write (also abbreviated as COW), as the name suggests, actually copies a copy of the memory for modification when writing. COW was first used in *nix systems to optimize thread and memory usage, and was later widely used in various programming languages, such as C's STL, etc.
In the PHP kernel, COW
is also the main memory optimization method. In the previous discussion about variables and memory, reference counting plays a crucial identification role in the destruction and recycling of variables. The purpose of reference counting is to enable COW to operate normally, thereby achieving optimal use of memory.
Advantages of copy-on-write:
is that when assigning a value to a variable, it will not apply for new memory to store the value saved by the new variable, but simply The memory is shared through a counter. Only when the value of the variable pointed to by one of the references changes, new space is allocated to save the value content to reduce memory usage.
From the underlying basic data structure of PHP
ref_count and is_ref are defined in the zval structure;
is_ref identifies whether the user uses & as a mandatory reference ;
ref_count is a reference count, used to identify how many variables this zval is referenced, that is, an automatic reference copied when writing. When it is 0, it will be destroyed.
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of What does Copy on write mean in PHP?. For more information, please follow other related articles on the PHP Chinese website!