Home  >  Article  >  Backend Development  >  PHP Features Garbage Collection Mechanism 1—Basic Knowledge of Reference Counting

PHP Features Garbage Collection Mechanism 1—Basic Knowledge of Reference Counting

黄舟
黄舟Original
2017-02-04 09:46:451263browse

Each php variable exists in a variable container called "zval". A zval variable container contains, in addition to the type and value of the variable, two bytes of additional information. The first one is "is_ref", which is a bool value used to identify whether this variable belongs to the reference set. Through this byte, the PHP engine can distinguish ordinary variables from reference variables. Since PHP allows users to use custom references by using &, there is also an internal reference counting mechanism in the zval variable container to optimize memory usage. The second extra byte is "refcount", which is used to indicate the number of variables (also called symbols) pointing to this zval variable container. All symbols exist in a symbol table, where each symbol has a scope (scope), the main script (for example: the script requested through the browser) and each function or method also have a scope.
When a variable is assigned a constant value, a zval variable container will be generated, as in the following example:

Example #1 Create a new zval container

<?php  
    $a = "new string";  
?>

The above is the content of the garbage collection mechanism 1 of PHP characteristics - the basic knowledge of reference counting. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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