PHP garbage collection mechanism

Source: Internet
Author: User
Tags count current time garbage collection variables reference variable thread

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.

So how do we 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.

Look at the example below

Example 1:gc.php
<?php
Error_reporting (E_all);
$a = ' I am Test. '
$b = & $a;

echo $b. " \ n ";
?>

Needless to say% php-f gc.php output is very clear:
hy0kl% php-f gc.php
I am Test.

Okay, Next:
Example 2:
<?php
Error_reporting (E_all);
$a = ' I am Test. '
$b = & $a;

$b = ' I'll change? ';

echo $a. " \ n ";
echo $b. " \ n ";
?>
The results of the implementation are still apparent:
hy0kl% php-f gc.php
I'll change?
I'll change?

June, please see:
Example 3:
<?php
Error_reporting (E_all);
$a = ' I am Test. '
$b = & $a;

unset ($a);

echo $a. " \ n ";
echo $b. " \ n ";
?>
Do you have to think about it?
hy0kl% php-f gc.php
notice:undefined variable:a in/usr/local/www/apache22/data/test/gc.php on line 8
I am Test.
Are you a little confused?

Look again:
Example 4:
<?php
Error_reporting (E_all);
$a = ' I am Test. '
$b = & $a;

Unset ($b);

echo $a. " \ n ";
echo $b. " \ n ";
?>
In fact, if Example 3 understand, this is similar to the same.
hy0kl% php-f gc.php
I am Test.
notice:undefined variable:b in/usr/local/www/apache22/data/test/gc.php on line 9

June and see:
Example 5:
<?php
Error_reporting (E_all);
$a = ' I am Test. '
$b = & $a;

$a = null;

echo ' $a = '. $a. " \ n ";
echo ' $b = '. $b. " \ n ";
?>
What is the first feeling of the fierce?
hy0kl% php-f gc.php
$a =
$b =
Yes, that's the output, Phper, which has a deep understanding of the PHP GC, doesn't feel strange, to be honest, when I first ran this code unexpectedly, it gave me a deeper understanding of the PHP GC. So the following work with the example of a natural good understanding.

Example 6:
<?php
Error_reporting (E_all);
$a = ' I am Test. '
$b = & $a;

$b = null;

echo ' $a = '. $a. " \ n ";
echo ' $b = '. $b. " \ n ";
?>

Here's a detailed analysis of GC and reference.
1. In all cases, a variable is created, which is a common point: it opens up a space in memory where a string I am test is stored. . PHP has a symbol table, used to record the block memory reference count, then the reference count of this memory plus 1, and with a label named $a (variable) point to the memory, easy to operate according to the label name to manipulate memory.

2. For the variable $a & operation, my understanding is to find $a the memory pointed to, and set the same reference point for $b, and will store the string I am test. Memory block in the symbol table to reference the Count plus 1. In other words, our script executes the string I am test when executing to this line. That memory has been quoted two times. Here to emphasize that & operation is to establish a reference point, not pointers, PHP does not have the concept of pointers! At the same time, it has been suggested that file soft links similar to UNIX can be understood to some extent: storing the character I am test. That memory is one of our real files, and the variables $a and $b are soft links for real files, but they point to the same real file. So, we see that in Example 2, the value of the $a is changed as well. Similar to manipulating files through a soft chain.

3. In Example 3 and 4, a unset () operation was performed. Based on the actual execution results, it can be seen that the unset () simply disconnects the variable from its original reference to the memory it points to, makes the variable itself a null-defined reference, emits a Notice on the call, and causes the block to exist The reference count in the symbol table minus 1 does not affect other variables that point to this memory. In other words, the PHP engine will reclaim this memory only if the reference count in the symbol table within a block is 0 o'clock.

PHP Manual
4.0.0 unset () became an expression. (In PHP 3, unset () would always return 1).
What does that mean?
Look at the following code and its results:
<?php
Error_reporting (E_all);
$a = ' I am Test. '
$b = & $a;

unset ($a);
unset ($a);
unset ($a);

echo ' $a = '. $a. " \ n ";
echo ' $b = '. $b. " \ n ";
?>
hy0kl% php-f gc.php

notice:undefined variable:a in/usr/local/www/apache22/data/test/gc.php on line 10
$a =
$b = I am Test.
The first unset () operation has been disconnected, so subsequent operations do not affect the reference count of any memory in the symbol table.

4. Through Example 5 & 6, it can be clearly and unambiguously concluded that: the assignment null operation is rather fierce, and it directly counts the memory that the variable points to in the symbol number 0, and that memory is naturally recycled by the engine, and when it is reused, it may be used as a store of other information immediately, It may not have been used again. But anyway, all points to that memory variable will no longer be able to manipulate the reclaimed memory, and any variables that attempt to invoke it will return null.

<?php
Error_reporting (E_all);
$a = ' I am Test. '
$b = & $a;

$b = null;

echo ' $a = '. $a. " \ n ";
echo ' $b = '. $b. " \ n ";

if (null = = $a)
{
echo ' $a is null. ';
} else
{
Echo ' The type of $a is unknown. ';
}
?>
hy0kl% php-f gc.php
$a =
$b =
$a is null.

To sum up, fully explain why we look at Open source product source code, it is common to see some of the larger temporary variables, or to use the reuse information that is no longer invoked, to be set or to display a null assignment. It is the equivalent of UNIX directly to the real file, all pointing to its soft links naturally become empty chain.
In the discussion of these details before the idea of a lot of take for granted, in the actual implementation of the test code only to find: Oh, so!
The end of the paper to feel shallow, never know this matter to preach.



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.