Analysis of memory overflow instances referenced by PHP objects and Analysis of php instances

Source: Internet
Author: User

Analysis of memory overflow instances referenced by PHP objects and Analysis of php instances

Generally, one of the biggest advantages of using a scripting language is to use its automatic garbage collection mechanism to release memory. You do not need to release the memory after using the variable, because these PHP will help you.
Of course, we can call the unset () function as needed to release the memory, but this is usually not required.
However, in PHP, memory is not automatically released in at least one case, even when unset () is called manually (). Details can test PHP official website on Memory leakage analysis: Id = 33595.


If there is a mutual reference relationship between the two objects, such as "parent object-Sub-object", call unset () for the parent object () the memory of the parent object referenced in the sub-object is not released (even if the parent object is reclaimed by garbage collection ).

Are you confused? Let's take a look at the following code:

<?phpclass Foo { function __construct(){ $this->bar = new Bar($this); }}class Bar { function __construct($foo = null){ $this->foo = $foo; }}while (true) { $foo = new Foo(); unset($foo); echo number_format(memory_get_usage()) . " ";}?>

Run this code and you will see that the memory usage is getting higher and higher until the light is used up.

...33,551,61633,551,97633,552,33633,552,696PHP Fatal error: Allowed memory size of 33554432 bytes exhausted(tried to allocate 16 bytes) in memleak.php on line 17

This is not a problem for most PHP programmers. However, if you use a lot of referenced objects in a long-running Code, especially when the object is relatively large, the memory will be quickly exhausted.

Userland Solution

Although tedious and not elegant, a solution is provided in the previously mentioned link.
This solution uses a destructor method before releasing an object to achieve the goal. The Destructor method can clear all internal parent object references, that is, it can release the memory that would have been overflows.

The following is the "fixed" code:

<?phpclass Foo { function __construct(){ $this->bar = new Bar($this); } function __destruct(){ unset($this->bar); }}class Bar { function __construct($foo = null){ $this->foo = $foo; }}while (true) { $foo = new Foo(); $foo->__destruct(); unset($foo); echo number_format(memory_get_usage()) . " ";}?>

Note the newly added Foo ::__ destruct () method and call $ foo->__ destruct () before releasing the object. Now this code solves the problem of increasing memory usage, so that the code can work well.

PHP kernel Solution

Why is memory overflow? I am not proficient in PHP kernel research, but it is certain that this problem is related to reference count.
The reference count for referencing $ foo in $ bar will not decrease because the parent object $ foo is released. PHP considers that you still need the $ foo object, so that this part of memory will not be released. The principle is roughly the same.

In general, a reference count is not decreasing, so some memory will never be released.
In addition, in the link mentioned above, it is pointed out that the process of modifying garbage collection will sacrifice a lot of performance, so you need to pay attention to this.

Instead of changing the garbage collection process, why don't unset () be used to release internal objects? (Or call _ destruct () when releasing an object ()?)
Maybe PHP kernel developers can modify this garbage collection mechanism here or elsewhere.

I believe this article will help you understand the PHP operating principles in depth.

How does a multi-level reference of a PHP Object reference it?

Class Fruit {
Protected $ con = array ();

Function aa (){
$ This-> con [0] = new Apple ();
$ This-> con [1] = new Banana ();
Echo $ this-> con [0]-> sweet;

Android memory overflow how to view what object resources are being referenced or occupied

Change the reference of a suspicious object to a weak reference. The weak reference can solve the problem of memory overflow.

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