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...