The PHP language, like other languages, has a garbage collection mechanism. So today we are going to explain to you about the PHP garbage collection mechanism of related issues. We hope to help you.
First, PHP garbage collection mechanism (garbage Collector referred to as GC)
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, and 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 PHP garbage collection mechanism
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
That means there is a 1% possibility to start the GC (that is, only 1 GCS in 100 requests will be started with one of the 100 requests).
PHP garbage collection mechanism is to scan all the session information, with the current time minus the last time the session was modified, the same Session.gc_maxlifetime parameter, if the lifetime exceeds Gc_maxlifetime (the default is 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?
1. Modify Session.save_path, or use Session_save_path () to save each site's session to a dedicated directory;
2. Provide GC start rate, naturally, PHP garbage collection mechanism to improve the start rate, the system performance will be correspondingly reduced, not recommended.
3 To determine the lifetime of the current session in the code, using Session_destroy () deleted.
Extended reading:
Discussion on the evolution of garbage collection algorithm (garbage Collection) in PHP5
PHP session expiration mechanism and configuration
PHP garbage collection mechanism prevents memory overflow