Analysis of memory leakage caused by Recursive Reference of PHP Objects. php recursion _ PHP Tutorial

Source: Internet
Author: User
Tags mysql functions
Recursive Reference of PHP Objects causes memory leakage analysis and php recursion. Recursive Reference of PHP Objects causes memory leakage analysis. php recursion usually results in memory leakage if PHP Objects have recursive references. This Bug has existed in PHP for a long time. Recursive Reference of PHP Objects causes memory leakage analysis and php recursion.

Generally, if PHP Objects have recursive references, memory leakage may occur. This Bug has existed in PHP for a long time. let's reproduce it first. the sample code is as follows:

<?phpclass Foo {  function __construct() {    $this->bar = new Bar($this);  }}class Bar {  function __construct($foo) {    $this->foo = $foo;  }}for ($i = 0; $i < 100; $i++) {  $obj = new Foo();  unset($obj);  echo memory_get_usage(), "/n";}?> 

Run the above code and you will find that the memory usage should not change, but it is actually increasing, and the unset does not take effect completely.

Most of the current development is based on the framework. if there is a complex object relationship in the application, you may encounter such a problem. let's take a look at the appropriate measures:

<?phpclass Foo {  function __construct() {    $this->bar = new Bar($this);  }  function __destruct() {    unset($this->bar);  }}class Bar {  function __construct($foo) {    $this->foo = $foo;  }}for ($i = 0; $i < 100; $i++) {  $obj = new Foo();  $obj->__destruct();  unset($obj);  echo memory_get_usage(), "/n";}?>

The solution is a bit ugly, but it is finally a battle. FortunatelyThis Bug has been fixed in the CVS code of PHP5.3..

In this regard, it is necessary to pay attention to the PHP program design! I believe this article has some reference value for everyone's PHP program design.


PHP recursion

Function demo has two inevitable output statements: the first and the last!
Function running process
1. output num
2. determine if num is greater than 0 and whether to call the function.
3. output num

When num is greater than 0, every time you call a function, you can only execute step 1. then, wait for the second step to call its own returned results and continue execution. therefore, after each call ends and returns, the num is output again, that is, the value following the result is output in reverse order!

Php recursive call

Well, just give me a few comments.

When writing SQL statements, it is best to write mysql functions in uppercase. in this case, SQL statements can be clearly described.
For example, $ SQL = "SELECT * FROM 'user' WHERE 'id' = '$ ID '";

In addition, table names and field names are all contained by vertices next to '1' (points under the English input method) and variables copied to the field are enclosed by 'single quotes for both string and numeric types.
But it's very good .....

In general, if PHP Objects have recursive references, memory leakage may occur. This Bug has existed in PHP for a long time ,...

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.