Home > Backend Development > PHP Tutorial > Analysis of memory leaks caused by recursive references to PHP objects, PHP recursion_PHP tutorial

Analysis of memory leaks caused by recursive references to PHP objects, PHP recursion_PHP tutorial

WBOY
Release: 2016-07-13 10:19:55
Original
917 people have browsed it

Analysis of memory leaks caused by recursive references to PHP objects, PHP recursion

Generally speaking, if there are recursive references to PHP objects, memory leaks will occur. This bug has existed in PHP for a long time. Let us reproduce this bug first. The sample code is as follows:

<&#63;php
class 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";
}
&#63;> 

Copy after login

Run the above code, you will find that the memory usage should remain unchanged, but in fact it keeps increasing, and unset does not fully take effect.

A lot of current development is based on frameworks. There are complex object relationships in the application, so you are likely to encounter such problems. Let’s take a look at what expedients can be done:

<&#63;php
class 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";
}
&#63;>

Copy after login

The method was a bit ugly, but it was finally dealt with. Fortunately this bug has been fixed in the CVS code of PHP5.3.

It is necessary to pay attention to this when designing PHP programs! I believe that what is described in this article has certain reference value for everyone's PHP program design.

Explanation of PHP recursion

The function demo has two inevitable output statements, one is the beginning one, and the other is the last one!
Function running process
1. Output num
2. Determine whether num is greater than 0 to determine whether to call the function
3. Output num

When num is greater than 0, each call The function only executes to the second step, and then waits for the second step to call itself to return the result before continuing to execute. Therefore, every time it calls itself and returns, num is output again, that is, the following values ​​​​are output in reverse order!

php recursive call

Okay, just a few words.

When writing sql statements, it is best to capitalize all mysql functions. This way, the sql statements will be clear at a glance and it will be nice to say.
For example: $sql = "SELECT * FROM `user` WHERE `id` = '$id'";

And the table name and field names all use the dot next to ` 1 (under the English input method Dot) and the variable copied to the field, whether it is a string or a numeric type, should be enclosed in ' single quotes
, which is very beneficial. . . . .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871099.htmlTechArticleAnalysis of memory leaks caused by recursive references to PHP objects, php recursion Generally speaking, if there are recursive references to PHP objects, it will A memory leak has occurred. This bug has existed in PHP for a long time,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template