PHP garbage collection mechanism----copy-on-write and reference counting

WBOY
Release: 2016-08-08 09:25:21
Original
1133 people have browsed it

  PHP uses reference counting and copy-on-write to manage memory. Copy-on-write ensures that copying values ​​between variables does not waste memory, and reference counting ensures that 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.

 The concept of symbol table:

 A variable consists of two parts: variable name and variable value. The symbol table is an array that maps variable names to the addresses of variable values ​​in memory.

 Copy as you write:

  When the value of one variable is copied to another variable, PHP does not use more memory for the copied value. Instead, it adds a new symbol table to indicate that the two variables belong to the same block of memory. So the code below doesn't actually create a new array:

   

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

    $other = $people; ///The array is not copied

   ?>

If you modify any of the copies, PHP will allocate the memory required to make the copy:

 $people[ 1 ] = "Tom"; //The value changes, the array is copied, and new memory is allocated

 By delaying allocation and copying, PHP saves time and memory in many situations. This is copy-on-write.

 Citation count:

 Each position in the symbol table has a reference counter, and its value represents the number of ways to obtain that piece of memory.

 After initializing and assigning values ​​to $people and $other, the array points to the symbol table, and the value of the reference counter of $people and $other is 2. In other words, there are two ways to obtain that piece of memory: $people and $other.

 But 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, such as a function parameter When a local variable reaches the end of the function, the value of the application counter is decremented by 1. When a variable is assigned a value in other memory space, the reference count of the old value is decremented by 1. When the reference count reaches 0, its memory is released.

 This is reference counting.

 Reference counting is the preferred memory management method. Keep variable scopes within functions, pass them by value, and let reference counting take care of 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-on-write and reference counting, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template