Home  >  Article  >  Backend Development  >  PHP garbage collection mechanism prevents memory overflow_PHP tutorial

PHP garbage collection mechanism prevents memory overflow_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:34:22982browse

1. PHP Garbage Collection Mechanism (Garbage Collector, referred to as GC)

In PHP, there are no variables pointing to When this object is deleted, 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 executed when the garbage object is recycled.
unset destroys the variable pointing to the object, not the object.

3. Session and PHP garbage collection mechanism

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 decide whether to enable a GC based on the values ​​of the global variables session.gc_probability and session.gc_divisor. By default, session.gc_probability=1, session.gc_divisor =100, which means there are 1% possibility of starting GC (that is to say, only one GC out of 100 requests will be started with one of the 100 requests).

The job of the PHP garbage collection mechanism 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, the GC may have unexpected results when processing sessions at multiple sites. The reason is: when the GC is working, it will not distinguish the sessions of different sites.

So how to solve it at this time?

1. Modify session.save_path, or use session_save_path() to save the session of each site to a dedicated directory,
2. Provide the startup rate of GC, and naturally, the startup rate of PHP garbage collection mechanism If it is increased, the system performance will be reduced accordingly, which is not recommended.
3. Determine the survival time of the current session in the code and delete it using session_destroy().


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445985.htmlTechArticle1. PHP garbage collection mechanism (Garbage Collector, referred to as GC) In PHP, when there is no variable pointing to this object, This object becomes garbage. PHP will destroy it in memory; this is...
Statement:
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