PHP-new is an object but is not used. What is the status of the object in the memory?

Source: Internet
Author: User
For example: {code...} is like this:

Class foo {} $ foo = new foo (); echo 123 ;... # The $ foo object is not used in the future. Will it be automatically recycled by the garbage collection mechanism or will it be in the memory?

Reply content:

For example:

Class foo {} $ foo = new foo (); echo 123 ;... # The $ foo object is not used in the future. Will it be automatically recycled by the garbage collection mechanism or will it be in the memory?

Php gc is based on reference count, so $ foo will not be recycled and will remain in the memory until the request ends.

Even if it is only new, $ foo is not assigned, and new foo () is not necessarily gc. The gc trigger of php is conditional.

Reference

PHP has a variable container to store all PHP variables, and then has a separate identifier to count the number of variable references. When the number of references is 0, the variable is destroyed, which is equivalent to memory recycling.

Logically, this should be called a memory leak, because you do not display unset ($ foo), resulting in the reference count is always 1! However, PHP has a mechanism to prevent such Memory leakage. Therefore, the memory reclaim here is performed after the script is executed.

You can look at the specific system, this estimate will understand a lot of http://php.net/manual/zh/features.gc.php

I am just reasoning:
It should be said in two steps that the new Class should not trigger memory operations, but the value assignment actually causes memory allocation. So $ a = new Class and $ a = 5; there is actually no difference. Unless you delete it explicitly, it will continue until the process ends and be automatically recycled.

I am not very familiar with it.
If the object is not used after it is generated, it will be a struct with a reference number of 1.
After leaving the scope, the number of struct references is 0, and the memory will be reclaimed when the next automatic GC is performed.

It should be automatically recycled, just as the declared object is not used, php has an automatic recycle Mechanism

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.