PHP garbage collection mechanism

Source: Internet
Author: User
Tags scalar zend

PHP garbage Collection is ultimately the operation of variables and their associated memory objects.

So before discussing the garbage collection mechanism of PHP, let's briefly introduce the internal representations of variables and their memory objects in PHP (the representations in their C source code).

The PHP official document divides variables in PHP into two categories: scalar and complex.

Scalar types include Boolean, Integer, floating-point, and string;

Complex types include arrays, objects, and resources;

There is also a special null, which is not divided into any type, but becomes a single class.

The memory recycling algorithm used in PHP5.2 is the famous reference counting,

This algorithm is called "Reference counting", the idea is very intuitive and concise:

Each memory object is assigned a counter that is initialized to 1 when a memory object is established (so there is always a variable referencing this object).

Each time a new variable references this memory object, the counter is incremented by 1, and the counter decreases by 1 whenever a variable that references this memory object is reduced.

When the garbage collection mechanism is in operation, destroy and reclaim the memory that is occupied by all 0 memory objects.

The memory object in PHP is Zval, and the counter is refcount__gc.

The problem with reference counting is that when two or more objects reference each other to form a loop,

The counter of the memory object is not reduced to 0;

At this time, this group of memory objects is useless, but can not be recycled, resulting in memory leaks;

php5.3 started with a new garbage collection mechanism,

First, PHP allocates a fixed-size "root buffer",

This buffer is used to hold a fixed number of zval,

This number is 10,000 by default,

If you need to modify it, you need to modify the constants in the source code ZEND/ZEND_GC.C gc_root_buffer_max_entries and then recompile.

By the above we can know that

A zval If there is a reference, it is either referenced by a symbol in the global symbol table or by another symbol in a zval that represents a complex type.

So there are some possible roots (root) in the zval.

Here we will not discuss how PHP found these possible roots, this is a very complex problem,

In short, PHP has a way to find these possible root zval and put them into the root buffer.

When the root buffer is in fulfilment, PHP performs garbage collection, which is the following algorithm:

1, the root in each root buffer zval follow the depth-first traversal algorithm to traverse all the zval that can be traversed,

and reduce the refcount of each zval by 1, and to avoid multiple minus 1 for the same zval.

(because a different root can traverse to the same zval), it is marked as "reduced" each time it is reduced by 1 to a zval.

2. Once again, the root zval depth in each buffer is first traversed,

If the refcount of a zval is not 0, add 1 to it, or leave it at 0.

3. Empty all roots in the root buffer (note that these zval are purged from the buffer instead of destroying them).

Then destroy all RefCount to 0 zval and retract its memory.

It doesn't matter if you don't fully understand it, just remember that the PHP5.3 garbage collection algorithm has the following characteristics:

1, not every time the RefCount reduced to enter the recovery cycle, only the root buffer after the fulfilment of the garbage collection at the beginning.

2, can solve the circular reference problem.

3. You can always keep the memory leak below a threshold value.

PHP configuration related to garbage collection algorithm

You can turn on or off the PHP garbage collection mechanism by modifying the zend.enable_gc in php.ini.

You can also turn on or off the garbage collection mechanism of PHP by calling gc_enable () or gc_disable ().

Even if the garbage collection mechanism is turned off in PHP5.3, PHP will still record the possible root-to-root buffer.

PHP does not automatically run garbage collection when the root buffer is in its fulfilment.

Of course, you can force memory reclamation at any time by calling the Gc_collect_cycles () function manually.

Original link http://www.cnblogs.com/leoo2sk/archive/2011/02/27/php-gc.html

PHP garbage collection mechanism

Related Article

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.