PHP garbage collection mechanism to prevent memory overflow _ PHP Tutorial

Source: Internet
Author: User
The PHP garbage collection mechanism prevents memory overflow. I. PHP garbage collection mechanism (GarbageCollector for short, GC) in PHP, when no variable points to this object, this object becomes garbage. PHP will destroy it in the memory; this is

I. PHP Garbage collection mechanism (GC)

In PHP, when no variable points to this object, this object becomes garbage. PHP will destroy it in the memory. this is the GC mechanism of PHP 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. GC processes generally start to run with each SESSION. gc aims to automatically destroy and delete these files after the session file expires.

II. _ destruct/unset

The _ destruct () destructor is executed when the garbage object is recycled.
Unset destroys the variable pointing to the object, not the object.

III. 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 use the global variable session. gc_probability and session. gc_pisor value to determine whether to enable a GC. by default, session. gc_probability = 1, session. gc_pisor = 100 that means there is a 1% possibility to start GC (that is to say, only one gc in the 100 requests will start with one of the 100 requests ).

The function of the PHP garbage collection mechanism is to scan all Session information and subtract the last modification time of the session from the current time. the gc_maxlifetime parameter is compared. if the survival time exceeds gc_maxlifetime (24 minutes by default), the session is deleted.
However, if your Web server has multiple sites and multiple sites, the GC processing session may have unexpected results, because: When GC is working, sessions of different sites are not distinguished.

How can this problem be solved?

1. modify session. save_path, or use session_save_path () to save the session of each site to a dedicated directory,
2. the GC startup rate is provided. Naturally, the startup rate of the PHP garbage collection mechanism is improved, and the system performance is also reduced accordingly. it is not recommended.
3. determine the survival time of the current session in the code and delete it using session_destroy.


Garbage collection mechanism (GC) in PHP, when no variable points to this object, this object becomes Garbage. PHP will destroy it in memory; this is...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.