Explanation of the garbage collection mechanism in PHP

Source: Internet
Author: User
This article describes how to read the garbage collection mechanism in PHP. Whether GC is available is an important concern in programming language development. For more information, see

This article describes how to read the garbage collection mechanism in PHP. Whether GC is available is an important concern in programming language development. For more information, see

Basic GC concepts of PHP
Like other languages, PHP has a garbage collection mechanism. So what we want to explain today is about the PHP garbage collection mechanism. Hope to help you. PHP strtotime Application Experience PHP memory_get_usage () managing memory PHP unset global variables usage details PHP unset () function destruction variables teach you how to quickly implement PHP full site permission verification 1. 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 run with each SESSION. gc aims to automatically destroy and delete session files after they expire. 2. The _ destruct/unset _ 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 follow 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.


Reference count Basics
Each php variable is stored in a variable container called "zval. A zval variable container not only contains the type and value of the variable, but also contains two bytes of additional information. the first is "is_ref", which is a bool value to identify whether the variable belongs to the reference set ). with this byte, the php engine can distinguish common variables from referenced variables. since php allows users to use & to use custom references, the zval variable container also has an internal reference counting mechanism to optimize memory usage. the second additional byte is "refcount", which indicates the number of variables (also called symbol) pointing to the zval variable container.

When a variable is assigned a constant value, a zval variable container is generated, as shown in the following example:

<? Php $ a = "new string";?>


In the preceding example, the new variable is a, which is generated in the current scope. A variable container of the string type and the new string value is generated. in the additional two bytes, "is_ref" is set to false by default because no custom reference is generated. "refcount" is set to 1 because only one variable uses this variable container. call xdebug to check the variable content:

<? Php $ a = "new string"; xdebug_debug_zval ('A');?>


The above code will output:

A: (refcount = 1, is_ref = 0) = 'new string'


Add a reference count for variable

<? Php $ a = "new string"; $ B = $ a; xdebug_debug_zval ('A');?>


The above code will output:

A: (refcount = 2, is_ref = 0) = 'new string'


At this time, the number of references is 2, because the same variable container is associated with variable a and variable B. php does not copy the generated variable container when it is not necessary. the variable container is destroyed when "refcount" is changed to 0. when any variable that is easily associated with a variable leaves its scope (for example, function execution ends), or the unset () function is called for the variable, "refcount" is reduced by 1, the following example shows how:

<? Php $ a = "new string"; $ B = $ c = $ a; xdebug_debug_zval ('A'); unset ($ B, $ c ); xdebug_debug_zval ('A');?>


The above code will output:

A: (refcount = 3, is_ref = 0) = 'new string' a: (refcount = 1, is_ref = 0) = 'new string'


If we execute unset ($ a) Now, the container containing the type and value $ will be deleted from the memory.

Compound types)

When considering a composite type such as array and object, things will be a little complicated. unlike values of the scalar type, array and object variables store their members or attributes in their own symbol tables. this means that the following example will generate three zval variable containers

<? Php $ a = array ('meaning' => 'LIFE', 'number' => 42); xdebug_debug_zval ('A');?>


The above code output:

A: (refcount = 1, is_ref = 0) = array ('mein' => (refcount = 1, is_ref = 0) = 'LIFE ', 'number' => (refcount = 1, is_ref = 0) = 42)


The three zval variable containers are: a, meaning, number. The rules for increasing and decreasing refcount are the same as those mentioned above.


When an array is added as an array element:

<? Php $ a = array ('one'); $ a [] = & $ a; xdebug_debug_zval ('A');?>


The output result of the above Code:

A: (refcount = 2, is_ref = 1) = array (0 => (refcount = 1, is_ref = 0) = 'one', 1 => (refcount = 2, is_ref = 1) = ...)


The variable container refcount pointed to by element a [1] of array a and array itself is 2.

When the unset function is called for Array $ a, the refcount of $ a changes to 1, causing memory leakage.

Cleaning variable containers
Although no symbols in a specific scope point to this structure (that is, the variable container), the container cannot be eliminated because the array element "1" still points to the array itself. because there is no other symbol pointing to it, the user cannot clear this structure, the result will cause memory leakage. fortunately, php will clear this data structure at the end of the request, but it will consume a lot of memory space before php clears it.


Recycling cycle
5.3.0PHP uses a new synchronization cycle recovery algorithm to handle the memory leakage mentioned above

First, we need to create some basic rules:
If a reference count increases, it will continue to be used, and of course it will not be in the garbage. if the reference technology is reduced to zero, the variable container will be cleared (free ). that is to say, grabage cycle is generated only when the reference count is reduced to a non-zero value ). second, in a garbage cycle, check whether the reference count is reduced by 1, and check which variable containers reference zero times to find which part is garbage

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.