PHP garbage collection mechanism

Source: Internet
Author: User
Every computer language has its own automatic garbage collection mechanism, so that programmers do not have to worry too much about program memory allocation. php is no exception, but in object-oriented programming (OOP, some objects need to be explicitly destroyed to prevent program execution memory overflow. I. PHP Garbage collection mechanism (Garbage... "> <LINKhref =" http://www.php100.com//statics/sty

 

Every computer language has its own automatic garbage collection mechanism, so that programmers do not have to worry too much about program memory allocation. php is no exception, but in object-oriented programming (OOP, some objects need to be explicitly destroyed to prevent program execution memory overflow.
I. 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 start to run with each SESSION. gc aims to automatically destroy and delete these files after the session file expires.

II. _ destruct/unset
The _ destruct () destructor is executed when the garbage object is recycled.
Unset destroys the variable pointing to the object, not the object.

III. Session and GC
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 use the global variable session. gc_probability and session. gc_divisor value to determine whether to enable a GC. By default, session. gc_probability = 1, session. gc_divisor = 100 that is to say, 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 ).
GC is used 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 GC startup rate is improved, and the system performance is also reduced accordingly. This is not recommended.
3. Determine the survival time of the current session in the code and delete it using session_destroy.

See the following example.

Example 1: gc. php
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

Echo $ B. "\ n ";
?>

Needless to say, the output of % php-f gc. php is very clear:
Hy0kl % php-f gc. php
I am test.

Okay. Next:
Example 2:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

$ B = 'I will change? ';

Echo $ a. "\ n ";
Echo $ B. "\ n ";
?>
The execution results are still obvious:
Hy0kl % php-f gc. php
I will change?
I will change?

Please refer:
Example 3:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

Unset ($ );

Echo $ a. "\ n ";
Echo $ B. "\ n ";
?>
Do you have to try it out?
Hy0kl % php-f gc. php
Notice: Undefined variable: a in/usr/local/www/apache22/data/test/gc. php on line 8
I am test.
A little confused?

Let's look at it again:
Example 4:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

Unset ($ B );

Echo $ a. "\ n ";
Echo $ B. "\ n ";
?>
In fact, if Example 3 understands this, it is similar.
Hy0kl % php-f gc. php
I am test.
Notice: Undefined variable: B in/usr/local/www/apache22/data/test/gc. php on line 9

You can also see:
Example 5:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

$ A = null;

Echo '$ a ='. $ a. "\ n ";
Echo '$ B ='. $ B. "\ n ";
?>
What is the first feeling of fierce competition?
Hy0kl % php-f gc. php
$ A =
$ B =
Yes, this is the output result. phper, which has a deep understanding of php gc, does not feel any strange. To be honest, it was very unexpected when I ran this code for the first time, but it gave me a deeper understanding of php gc. the examples below are similar to those below.

Example 6:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

$ B = null;

Echo '$ a ='. $ a. "\ n ";
Echo '$ B ='. $ B. "\ n ";
?>

Next we will analyze GC and references in detail.
1. in all the examples, a variable is created. In other words, a space is opened up in the memory and a string I am test is stored in it .. PHP has a symbol table to record the reference count of each block of memory. In this case, the reference count of this block of memory is added to 1 and a tag named $ a (variable) is used) point to this memory to facilitate memory operation according to the tag name.

2. perform the & operation on the variable $ a. In my understanding, find the memory that $ a points to, create the same reference for $ B, and store the string I am test. memory block in the symbol table reference count plus 1. in other words, when the script runs to this line, it stores the string I am test. the memory is referenced twice. what should be emphasized here is that & the operation creates a reference point, not a pointer. PHP does not have the pointer concept! At the same time, it was suggested that file soft links are similar to those of UNIX. it can be understood to a certain extent: store the character I am test. and $ a and $ B are soft links for real files, but they point to the same real file. so, we can see that while assigning values to $ B in Example 2, the value of $ a also changes. similar to operating a file through a soft link.

3. in examples 3 and 4, the unset () operation is performed. according to the actual execution results, it can be seen that unset () only disconnects the reference of the variable to the memory that it originally pointed to, making the variable itself a null reference without definition, the Notice is issued during the call, and the reference count in the symbol table is reduced by 1, which does not affect other variables pointing to this memory. in other words, the PHP engine will reclaim the memory only when the reference count in the symbol table is 0.

PHP Manual
4.0.0 unset () became an expression. (In PHP 3, unset () wowould always return 1 ).
What does this mean?
Let's take a look at the following code and its results:
Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

Unset ($ );
Unset ($ );
Unset ($ );

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 will not affect any memory reference records of the symbol table.

4. you can use Example 5 & 6 to make it clear that the null assignment operation is quite fierce, and it will directly set the reference count in the symbol number pointed to by the variable to 0, the memory is naturally recycled by the engine. It is unknown when it will be reused. It may be immediately used to store other information and may never be used again. however, in any case, all the previously directed memory variables will no longer be able to operate on the recycled memory, and any variable trying to call it will return null.

Error_reporting (E_ALL );
$ A = 'I am test .';
$ B = & $;

$ B = null;

Echo '$ a ='. $ a. "\ n ";
Echo '$ B ='. $ B. "\ n ";

If (null ===$)
{                                                                               
Echo '$ a is null .';
} Else

Echo 'the type of $ a is unknown .';

?>
Hy0kl % php-f gc. php
$ A =
$ B =
$ A is null.

In summary, it fully explains why we often see some large temporary variables when looking at the source code of open-source products, or the reuse information that is no longer called after use will be set or the displayed value is null. it is equivalent to killing real files directly in UNIX, and all soft links pointing to it naturally become empty chains.
I have thought a lot about these details before. After I actually executed the test code, I found out: Oh, that's it!
On the paper, you may have to learn more about it.

 

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.