First, the PHP garbage collection mechanism (garbage Collector abbreviation GC)
In PHP, when no variable points to this object, the object becomes garbage. PHP will destroy it in memory; this is the GC garbage disposal mechanism in 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. The GC process typically runs with each session. The purpose of the GC is to automatically destroy the deleted files after they expire.
Second, __destruct/unset
A __destruct () destructor that executes when a garbage object is reclaimed.
Unset destroys a variable that points to an object, not the object.
Iii. Session and GC
Because of the working mechanism of PHP, it does not have a daemon thread that periodically scans session information and determines whether it is invalidated, and when a valid request occurs, PHP will session.gc_probability and session.gc_ according to the global variables Divisor value to determine whether to enable a GC, by default, Session.gc_probability=1, Session.gc_divisor = 100, which means 1% probability to start GC ( This means that only one GC in 100 requests will be started with one of 100 requests.
The GC's job is to scan all session information, using the current time minus the last modification time of the session, compared to 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 session may have unexpected results, because the GC at work, does not distinguish between different site sessions.
So how do we solve this time?
1. Modify the Session.save_path, or use Session_save_path () to save each session of the site to a dedicated directory,
2. To provide a GC start-up rate, naturally, the GC's startup rate increased, the system performance will be reduced, not recommended.
3. In the code to determine the current session life time, using Session_destroy () Delete.