PHP Object Recursive reference causes memory leak analysis, PHP recursive _php tutorial

Source: Internet
Author: User

PHP Object Recursive reference causes memory leak analysis, PHP recursive


Typically, a memory leak occurs if there is a recursive reference to the PHP object. This bug has been in PHP for a long time, let us reproduce the bug, 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 < $i + +) {  $obj = new Foo ();  Unset ($obj);  

Run the above code, you will find that the memory usage should have been the same, but in fact it is constantly increasing, unset not fully effective.

Now many of the development is based on the framework, there are complex object relationships in the application, then it is likely to encounter such a problem, the following to see what expedient:

<?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 < $i + +) {  $obj = new Foo ();  $obj->__destruct ();  Unset ($obj);  Echo memory_get_usage (), "/n";}? >

It was a little ugly, but it was finally dealt with. Fortunately, this bug has been fixed in PHP5.3 's CVS code .

In this case, it is necessary to pay attention to PHP programming! It is believed that this article has certain reference value to the PHP program design of everybody.


Explanation of PHP recursion

The function demo has two necessary output statements, one is the beginning one, one is the last one!
Function Run Flow
1. Output num
2. Determine if NUM is greater than 0 no so that the function is called
3. Output num

When NUM is greater than 0 o'clock, each call to the function executes only to the 2nd step, and then waits for the second pace to continue executing after returning the result with itself, so that every time the call itself returns, it outputs the NUM again, that is, the back value of the reverse output!

PHP recursive invocation

All right, just a few words.

When writing SQL statements, it's best to capitalize the MySQL function in such a way that SQL statements look good at a glance.
For example: $sql = "SELECT * from ' user ' WHERE ' id ' = ' $id '";

and the table name field names are included with the dot next to ' 1 (the point below the English input method) and the variables that are copied to the field are enclosed in single quotation marks whether they are strings or numeric types.
But it's good.

http://www.bkjia.com/PHPjc/871099.html www.bkjia.com true http://www.bkjia.com/PHPjc/871099.html techarticle PHP Object Recursive reference causes memory leak analysis, PHP recursion Typically, a memory leak occurs if there is a recursive reference to the PHP object. This bug has existed in PHP for a long time, ...

  • 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.