Every computer language has its own automatic garbage collection mechanism, so that programmers do not have to worry too much about program memory allocation. php is no exception, but in Object-Oriented Programming (OOP, some objects need to be explicitly destroyed to prevent program execution memory overflow.
I. PHP Garbage collection mechanism (GC)
In PHP, when no variable points to this object, this object becomes garbage. PHP will destroy it in the memory. This is the GC mechanism of PHP to prevent memory overflow. When a PHP thread ends, all memory space currently occupied will be destroyed, and all objects in the current program will be destroyed at the same time. GC processes generally run with each SESSION. Gc aims to automatically destroy and delete session files after they expire.
Ii. _ destruct/unset
The _ destruct () destructor is executed when the garbage object is recycled. Unset destroys the variable pointing to the object, not the object.
Iii. Session and GC
Due to the working mechanism of PHP, it does not have a daemon thread to regularly scan Session information and determine whether it is invalid. When a valid request occurs, PHP will use the global variable session. gc_probability and session. gc_divisor value to determine whether to enable a GC. By default, session. gc_probability = 1, session. gc_divisor = 100 that is to say, there is a 1% possibility to start GC (that is to say, only one gc in the 100 requests will start with one of the 100 requests ).
GC is used to scan all Session information and subtract the last modification time of the session from the current time. the gc_maxlifetime parameter is compared. If the survival time exceeds gc_maxlifetime (24 minutes by default), the session is deleted. However, if your Web server has multiple sites and multiple sites, the GC processing session may have unexpected results, because: When GC is working, sessions of different sites are not distinguished.
How can this problem be solved?
- Modify session. save_path, or use session_save_path () to save the session of each site to a dedicated directory.
- The GC startup rate is provided. Naturally, the GC startup rate is improved, and the system performance is also reduced. This is not recommended.
- Determine the survival time of the current session in the Code and delete it using session_destroy.
See the following example:
Example 1: gc. php
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; echo $b ."n"; ?>
Needless to say, the output of % php-f gc. php is very clear:
hy0kl% php -f gc.php I am test.
Okay. Next:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; $b = 'I will change?'; echo $a ."n"; echo $b ."n"; ?>
The execution results are still obvious:
hy0kl% php -f gc.php I will change?I will change?
Please refer:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; unset($a); echo $a ."n"; echo $b ."n";?>
Do you have to try it out?
hy0kl% php -f gc.php Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 8I am test.
Let's look at it again:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; unset($b); echo $a ."n"; echo $b ."n";?>
In fact, if Example 3 understands this, It is similar.
hy0kl% php -f gc.php I am test.Notice: Undefined variable: b in /usr/local/www/apache22/data/test/gc.php on line 9
You can also see:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; $a = null; echo '$a = '. $a ."n"; echo '$b = '. $b ."n"; ?>
What is the first feeling of fierce competition?
hy0kl% php -f gc.php $a = $b =
Yes, this is the output result. phper, which has a deep understanding of php gc, does not feel any strange. To be honest, it was very unexpected when I ran this code for the first time, but it gave me a deeper understanding of php gc. The examples below are similar to those below.
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; $b = null; echo '$a = '. $a ."n"; echo '$b = '. $b ."n"; ?>
Next we will analyze GC and references in detail:
In all examples, a variable is created. In other words, a space is opened up in the memory and a string I am test is stored in it. . PHP has a symbol table to record the reference count of each block of memory. In this case, the reference count of this block of memory is added to 1 and a tag named $ a (variable) is used) point to this memory to facilitate memory operations according to the tag name.
Perform the & Operation on the variable $ a. In my understanding, find the memory to which $ a points, create the same reference to $ B, and store the string I am test. In the symbol table, add 1 to the reference count. In other words, when the script runs to this line, it stores the string I am test. The memory is referenced twice. What should be emphasized here is that & the operation creates a reference point, not a pointer. PHP does not have the pointer concept! At the same time, it was suggested that file soft links are similar to those of UNIX. It can be understood to a certain extent: store the character I am test. And $ a and $ B are soft links for real files, but they point to the same real file. So, we can see that while assigning values to $ B in Example 2, the value of $ a also changes. Similar to operating a file through a soft link.
In Examples 3 and 4, the unset () operation is performed. According to the actual execution results, it can be seen that unset () Only disconnects the reference of the variable to the memory that it originally pointed to, making the variable itself a null reference without definition, the Notice is issued during the call, and the reference count in the symbol table is reduced by 1, which does not affect other variables pointing to this memory. In other words, the PHP engine will reclaim this memory only when the reference count in a symbol table is 0.
Let's take a look at the following code and its results:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; unset($a); unset($a); unset($a); echo '$a = '. $a ."n"; echo '$b = '. $b ."n"; ?>
Output:
hy0kl% php -f gc.php Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 10$a = $b = I am test.
The first unset () operation has been disconnected, so subsequent operations will not affect any memory reference records of the symbol table.
The null Value assignment operation is very fierce. It directly sets the reference count in the symbol number pointed to by the variable to 0, and the memory is naturally recycled by the engine, it is unknown when it will be reused. It may be immediately used to store other information and may never be used again. However, in any case, all the previously directed memory variables will no longer be able to operate on the recycled memory, and any variable trying to call it will return null.
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; $b = null; echo '$a = '. $a ."n"; echo '$b = '. $b ."n"; if (null === $a) { echo '$a is null.'; } else { echo 'The type of $a is unknown.'; } ?>
Output:
hy0kl% php -f gc.php $a = $b = $a is null.
In summary, it fully explains why we often see some large temporary variables when looking at the source code of open-source products, or reuse information that is no longer called after use will be set or the displayed value is null. It is equivalent to killing real files directly in UNIX, and all soft links pointing to it naturally become empty chains.
I have thought a lot about these details before. After I actually executed the test code, I found out: Oh, that's it! The end of the paper is shallow, and I know that this matter must be done.