PHP reference counter for garbage collection mechanism Introduction _php tutorial

Source: Internet
Author: User
PHP has a very simple garbage collector that actually garbage collects objects that are no longer in the memory scope (scope). The internal way of garbage collection is to use a reference counter, so when the counter reaches 0 o'clock (meaning that a reference to the object is not available), the object is garbage collected and removed from memory.

Each computer language has its own automatic garbage collection mechanism, so that programmers do not have to be overly concerned about program memory allocation, PHP is no exception, but in object-oriented programming (OOP) programming, some objects need to be explicitly destroyed, to prevent the program to perform memory overflow.

first, PHP garbage collection mechanism (garbage Collector referred to as GC)

In PHP, when there are no variables pointing to this object, the object becomes garbage. PHP will destroy it in memory, which is the GC garbage disposal mechanism of 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. GC processes are typically run with each session. The GC is designed to automatically destroy deleted files after the session file expires.

Second, __destruct/unset

The __destruct () destructor is executed when the garbage object is reclaimed.
Unset destroys a variable that points to an object, not the object.

third, Session and GC

Because of PHP's working mechanism, it does not have a daemon thread to periodically scan the session information and determine whether it is invalid, when a valid request occurs, PHP will be based on global variables session.gc_probability and SESSION.GC_ The value of divisor to determine whether a GC is enabled, by default, Session.gc_probability=1, session.gc_divisor = 100 which means there is a 1% possibility to start the GC ( This means that only one GC in 100 requests starts with one of the 100 requests.

GC's job is to scan all the session information, the current time minus the last time the session was modified, compared with 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 sessions can have unexpected results because the GC does not differentiate between sessions at different sites when it is working.

So how is this time resolved?

1. Modify Session.save_path, or use Session_save_path () to save each site's session to a dedicated directory,
2. Provide GC start rate, naturally, the GC start rate increases, the system performance will be correspondingly reduced, not recommended.
3. Determine the lifetime of the current session in the code, using Session_destroy () to delete

http://www.bkjia.com/PHPjc/325967.html www.bkjia.com true http://www.bkjia.com/PHPjc/325967.html techarticle PHP has a very simple garbage collector that actually garbage collects objects that are no longer in the memory scope (scope). The internal way of garbage collection is to use a reference meter ...

  • 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.