php garbage collection mechanism

巴扎黑
Release: 2016-11-24 11:36:08
Original
1166 people have browsed it

Every computer language has its own automatic garbage collection mechanism, so that programmers do not have to worry too much about program memory allocation, and PHP is no exception. However, in object-oriented programming (OOP) programming, some objects need to be explicitly destroyed; to prevent Program execution memory overflow.
1. PHP Garbage Collection Mechanism (Garbage Collector (GC))
In PHP, when no variables point to this object, the object becomes garbage. PHP will destroy it in memory; this is PHP's GC garbage disposal mechanism 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. The GC process generally starts running with each SESSION. The purpose of gc is to automatically destroy and delete the session files after they expire.

2. __destruct /unset
__destruct() The destructor is used after the garbage object is recycled time execution.
unset destroys the variable pointing to the object, not the object.

3. 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 variables session.gc_probability and session The value of .gc_divisor determines whether to enable a GC. By default, session.gc_probability=1, session.gc_divisor =100 means there is a 1% possibility of starting a GC (that is, there is only one gc in 100 requests) Will be started with one of the 100 requests).
The job of GC is to scan all Session information, subtract the last modification time of the session from the current time, and compare it with the session.gc_maxlifetime parameter. If the survival time exceeds gc_maxlifetime (default 24 minutes), the session will be deleted.
However, if your web server has multiple sites, when there are multiple sites, GC may have unexpected results when processing sessions. The reason is: when GC is working, it will not distinguish between sessions of different sites.

So at this time How to solve it?
1. Modify session.save_path, or use session_save_path() to save the session of each site to a dedicated directory.
2. Provide the GC startup rate. Naturally, as the GC startup rate increases, the system performance will decrease accordingly. Not recommended.
3. Determine the survival time of the current session in the code and delete it using session_destroy().

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!