Analysis of references and garbage collection in PHP

Source: Internet
Author: User
Tags memory usage
This article brings to you the content of the PHP reference and garbage collection analysis, there is a certain reference value, the need for friends can refer to, I hope to help you.

Each PHP variable exists in a variable container called "Zval". A Zval variable container that includes two bytes of extra information in addition to the type and value of the variable. The first is "Is_ref", which is a bool value that identifies whether the variable belongs to a reference collection (reference set). With this byte, the PHP engine can differentiate between normal and reference variables, and since PHP allows users to use the custom reference by using &, there is an internal reference counting mechanism in the Zval variable container to optimize memory usage. The second extra byte is "RefCount", which represents the number of variables (also known as symbols) that point to the Zval variable container. All symbols exist in a symbol table, where each symbol has scope (scope), and those main scripts (for example, scripts that are requested by the browser) and each function or method also have scopes.

The object in PHP is passed as a reference

Is_ref = 0, refcount = 0 zval container will be destroyed at the end of script execution

Cite the official example

<?php
$a = Array (' one '), 
$a [] =& $a; 
Xdebug_debug_zval (' a ');
A: (refcount=2, Is_ref=1) =array (
   0 = (refcount=1, is_ref=0) = ' One ',
   1 = (refcount=2, is_ref=1) = ...
)

Executing unset$a will release the variables associated with Zval's memory but the closed loop itself still exists inside

(Refcount=1, Is_ref=1) =array (
   0 = (refcount=1, is_ref=0) = ' One ',
   1 = (refcount=1, is_ref=1) = ...
)

But at this point, no variable can be manipulated into the Zval container time has become a memory garbage does not release

Recycling mechanism: In a nutshell, after executing the script, the overall data of the remaining variables is all refcount-1, and if it is reduced to 0, it is determined that the garbage is destroyed by the memory container.

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.