PHP uses reference counts and write-time replication (Copy-on-write) to manage memory. Reference calculations ensure that memory is returned to the operating system when the reference is no longer needed, and copy-on-write ensures that no memory is wasted when values are copied between variables.
To understand memory management in PHP, you must first understand the idea of the symbol table, which has two parts – variable names (such as $name) and variable values (such as "Fred"). The symbol table is an array that maps the variable names to their values in memory.
When copying a value from one variable to another, PHP does not get more memory for copying the value, but instead updates the symbol table to indicate that "these two variables are the same memory name." So the following code does not actually create a new array:
$people = Array ("Gonn", "Zeng"); $programmer = $people; Array is not copied
If you modify any of the copies, then PHP allocates memory and generates the copy:
$people [1] = 26; Array is copied, value changes
Due to delayed allocation and replication, PHP saves time and memory in many cases. This is the write-time copy.
Each value that the symbol table points to has a reference count (reference count), which is a number that represents the number of paths to that memory. After assigning the initial value of an array to $people and assigning $people to $programmer, the entry in the symbol table to the array is $people and $programmer, and the reference count is 2. In other words, there are two ways to reach that memory: by $people or $programmer. But $people[1] after the change, PHP creates a new array for $people, and the reference count for each array is just 1.
When a variable is not in scope (a function parameter or a local variable at the end of a function), the reference count value is reduced by 1. When a variable is assigned a value in another area of memory, the old reference count value is reduced by 1. When the reference count value reaches 0, the memory is freed. This is the reference count.
The preferred method of referencing a counter-managed memory is to keep the value of the function local transfer function of the variable, and to have the reference count responsible for freeing the memory when the reference is no longer needed. You can use the function isset () and unset () If you want more information or have full control over the value of the release variable.
To see if a variable has been set (even if it is an empty string), use Isset ():
$s 1 = isset ($name); $s 1 is false$name = "gonn"; $s 2 = isset ($name); $s 2 is true
Use unset () to remove the value of a variable:
$name = "Gonn"; unset ($name); $name is null
Extended reading: http://php.net/manual/zh/features.gc.refcounting-basics.php
http://www.bkjia.com/PHPjc/752448.html www.bkjia.com true http://www.bkjia.com/PHPjc/752448.html techarticle PHP uses reference counts and write-time replication (Copy-on-write) to manage memory. Reference calculations ensure that memory is returned to the operating system when the reference is no longer needed, and copy-on-write ensures that the variables are duplicated ...