PHP garbage collection mechanism understanding _ PHP Tutorial

Source: Internet
Author: User
Understanding of PHP garbage collection mechanism. PHP garbage collection mechanism is only available after php5. I will introduce some understanding about PHP garbage collection mechanism and hope to help you. PHP garbage collection mechanism was available only after php5 before php5.3. next I will introduce some understanding about PHP garbage collection mechanism and hope to help you.

The garbage collection mechanism used before php 5.3 is a simple "reference count", that is, each memory object is allocated with a counter. when the memory object is referenced by a variable, counter 1; when the variable reference is removed, counter-1; when counter = 0, indicates that the memory object is not used, the memory object is destroyed, and garbage collection is completed.

There is a problem with "reference count", that is, when two or more objects reference each other to form a ring, the counter of the memory object will not be reduced to 0; at this time, this group of memory objects is useless, but cannot be recycled, resulting in memory leakage;

Php5.3 started with a new garbage collection mechanism. based on reference counting, it implemented a complex algorithm to detect the existence of reference rings in memory objects to avoid memory leakage.

This algorithm can be referred to in the following article, which is the main reference in this small Summary: Evolution of the Garbage Collection algorithm (Garbage Collection) in PHP5


See the following example.

Example 1: gc. php

The code is as follows:

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:

The code is as follows:

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:

The code is as follows:

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:

The code is as follows:

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:

The code is as follows:

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:

The code is as follows:

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

$ B = null;

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

This is a php Tutorial. you can go to Baidu to learn more.

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.