Each computer language has its own automatic garbage collection mechanism, so that programmers do not have to be overly concerned about program memory allocation, PHP is no exception, but in object-oriented programming (OOP) programming, some objects need to be explicitly destroyed, to prevent the program to perform memory overflow.
One, PHP garbage collection mechanism (garbage Collector referred to as GC) Forever Ying Casino
In PHP, when there are no variables pointing to this object, the object becomes garbage. PHP will destroy it in memory, which is the GC garbage disposal mechanism of PHP to prevent memory overflow. When a PHP thread ends, all memory space currently occupied is destroyed, and all objects in the current program are destroyed at the same time. GC processes are typically run with each session. The GC is designed to automatically destroy deleted files after the session file expires.
Second, __destruct/unset
The __destruct () destructor is executed when the garbage object is reclaimed. Unset destroys a variable that points to an object, not the object.
Third, Session and GC
Because of PHP's working mechanism, it does not have a daemon thread to periodically scan the session information and determine whether it is invalid, when a valid request occurs, PHP will be based on global variables session.gc_probability and SESSION.GC_ The value of divisor to determine whether a GC is enabled, by default, Session.gc_probability=1, session.gc_divisor = 100 which means there is a 1% possibility to start the GC ( This means that only one GC in 100 requests starts with one of the 100 requests.
GC's job is to scan all the session information, the current time minus the last time the session was modified, compared with the session.gc_maxlifetime parameter, if the survival time exceeds gc_maxlifetime (default 24 minutes), The session is deleted. However, if your Web server has multiple sites, multiple sites, GC processing sessions can have unexpected results because the GC does not differentiate between sessions at different sites when it is working.
So how is this time resolved?
- Modify the Session.save_path, or use Session_save_path () to save each site's session to a dedicated directory.
- To provide a GC start rate, natural, GC start rate increases, the system performance will be correspondingly reduced, not recommended.
- Determine the lifetime of the current session in the code, using Session_destroy () to delete.
Look at 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 php-f gc.php output is very clear:
hy0kl% php-f gc.php I am test.
OK, Next:
<?php error_reporting (E_all); $a = ' I am Test. '; $b = & $a; $b = ' I'll change? '; echo $a. " n "; echo $b. " n ";?>
The results of the implementation are still obvious:
hy0kl% php-f gc.php I'll change? I'll change?
June please look:
<?php error_reporting (E_all); $a = ' I am Test. '; $b = & $a; unset ($a); echo $a. " n "; echo $b. " n ";? >
Do you have to think about it?
hy0kl% php-f gc.php notice:undefined variable:a in/usr/local/www/apache22/data/test/gc.php on line 8I am test.
June Look 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 understand, this 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
June and look:
<?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 sensation of the fierce?
hy0kl% php-f gc.php $a = $b =
Yes, that's what the output is, and the phper of the PHP GC that has a deep understanding of it doesn't seem strange, to be honest, when I first ran this code, it was a surprise, but I got a deeper understanding of the PHP GC. So the following examples of co-workers are naturally well understood.
<?php error_reporting (e_all); $a = ' I am test '; $b = & $a; $b = null; Echo ' $a = '. $a. " n "; echo ' $b = '. $b. " n ";?>
Let's analyze the GC and the reference in detail:
In all the examples, a variable is created, and the process is a bit more popular: it opens up a space in memory where a string I am test is stored. There is a symbol table inside PHP to record each block memory reference count, then the reference count of this memory will be added 1, and a tag named $a (variable) point to the memory, convenient for the tag name to manipulate memory.
The & operation of the variable $a, I understand is to find the memory pointed to $a, and for the $b to establish the same reference point, and will hold the string I am test. The memory block in the symbol table refers to the Count plus 1. In other words, when our script executes to this line, it holds the string I am test. The memory of the block was quoted two times. It is emphasized here that the & operation is to establish a reference point, not a pointer, PHP does not have the concept of pointers! At the same time someone has proposed a file soft link similar to UNIX. This can be understood to some extent: store the character I am test. That memory is one of our real files, and the variables $a and $b are soft links built for real files, but they point to the same real file. So, as we can see, the value of the $a is changed as well, while the $b is assigned in Example 2. Similar to manipulating files through a soft chain.
In Example 3 and 4, a unset () operation was performed. According to the actual execution result, it can be seen that: unset () just disconnects the variable to its original point of reference to the memory, so that the variable itself is not defined by the null reference, the call issued when the Notice, and the existence of the symbol table within the reference count minus 1, and does not affect other points to the memory of the variable. In other words, the PHP engine reclaims this memory only if the reference count in the symbol table in the block is 0 o'clock.
Take a look at the following code with its result:
<?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 te St.
The first unset () operation has been disconnected, so subsequent operations do not affect the reference count of any memory in the symbol table.
The assignment of a null operation is quite fierce, and it will directly set the reference count of the memory that the variable points to in the symbol number to 0, which is naturally recycled by the engine, and when it is not known, it may be used as a store of other information, perhaps no longer used. However, all the memory variables that point to that block will no longer be able to manipulate the recovered memory, and any variables that attempt to invoke 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.
To sum up, fully explain why we are looking at the source code, often see some of the larger temporary variables, or use the end of no longer call reuse information will be set or display the assignment is null. It is equivalent to the UNIX directly to the real files out, all the soft links to it naturally become empty chain.
Before discussing these points of detail there are a lot of assumptions, after the actual implementation of the test code only to discover: Oh, that's it! It must be preach on paper.
A few simple programs to see the garbage collection mechanism of PHP