PHP garbage collection mechanism

Source: Internet
Author: User
Tags current time garbage collection

PHP has a very simple garbage collector that will actually garbage collect objects that are no longer in the memory range (scope). The internal way of garbage collection is to use a reference counter, so when the counter reaches 0 o'clock (meaning that references to the object are not available), the object is garbage collected and deleted from memory.

——————————————————————————————————————————————————————————–

Each computer language has its own automatic garbage collection mechanism, so that programmers do not have to care too much about program memory allocation, PHP is no exception, but in object-oriented programming (OOP) programming, some objects need to be explicitly destroyed, prevent the program from executing memory overflow.
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.

Then how to 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.

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.