Php memory management PHP uses reference computing and write copy php uses reference computing and write copy-on-write to manage memory. During write replication, make sure that no memory is wasted when copying values between variables. for reference calculation, make sure that the memory is returned to the operating system when the reference is no longer needed. To understand the memory management in PHP, you must first understand the idea of symboltable. the variable has two parts: variable name php memory management.
PHP uses reference computation and write-time replication
Php uses copy-on-write to manage memory. During write replication, make sure that no memory is wasted when copying values between variables. for reference calculation, make sure that the memory is returned to the operating system when the reference is no longer needed.
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:
$ Worker = array ("Fred", 35, "Wilma"); $ other = $ worker; // The array is not copied
If any copy is modified, PHP allocates memory and generates the copy:
$ Worker [1] = 36; // 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 the initial values of the array are assigned to $ worker and $ worker is assigned to $ other, the entry of the symbol table pointing to the array is $ worker and $ other, and the reference count is 2. In other words, there are two ways to reach that piece of memory: through $ worker or $ other. After $ worker [1] is changed, PHP creates a new array for $ worker, 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 = "Fred"; $ s2 = isset ($ name); // $ s2 is true
Use unset () to delete the value of a variable:
$ Name = "Fred ";Unset ($ name); // $ name is NULL