Home Backend Development PHP Tutorial Analysis of references and garbage collection in php

Analysis of references and garbage collection in php

Sep 04, 2018 pm 05:43 PM
Garbage collection Quote

The content of this article is about the analysis of references and garbage collection in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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.

//Objects in php are passed by reference

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

Quote from the official example

<?php
$a = array( &#39;one&#39; );
$a[] =& $a;
xdebug_debug_zval( &#39;a&#39; );
Copy after login
a: (refcount=2, is_ref=1)=array (
   0 => (refcount=1, is_ref=0)=&#39;one&#39;,
   1 => (refcount=2, is_ref=1)=...
)
Copy after login

自引用(curcular reference,自己是自己的一个元素)的数组的zval

Executing unset$a will release the memory association between the variable and zval, but the closed loop itself still exists

(refcount=1, is_ref=1)=array (
   0 => (refcount=1, is_ref=0)=&#39;one&#39;,
   1 => (refcount=1, is_ref=1)=...
)
Copy after login

Zvals after removal of array with a circular reference demonstrating the memory leak

But no variables can be manipulated to zval at this time. The container time has become memory garbage and cannot be released.

Recycling mechanism: To put it simply, after executing the script, the remaining Existing variables will be used for all refcount of the overall data -1. If it is reduced to 0, it will be judged as garbage and the memory container will be destroyed.

Related recommendations:

What kind of garbage can be recycled by PHP's garbage collection mechanism? Garbage collection cannot recycle garbage

PHP garbage collection mechanism—basic knowledge of reference counting

The above is the detailed content of Analysis of references and garbage collection in php. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use block quotes in Apple Notes How to use block quotes in Apple Notes Oct 12, 2023 pm 11:49 PM

In iOS 17 and macOS Sonoma, Apple has added new formatting options for Apple Notes, including block quotes and a new Monostyle style. Here's how to use them. With additional formatting options in Apple Notes, you can now add block quotes to your notes. The block quote format makes it easy to visually offset sections of writing using the quote bar to the left of the text. Just tap/click the "Aa" format button and select the block quote option before typing or when you are on the line you want to convert to a block quote. This option applies to all text types, style options, and lists, including checklists. In the same Format menu you can find the new Single Style option. This is a revision of the previous "equal-width"

How to avoid memory leaks in C# development How to avoid memory leaks in C# development Oct 08, 2023 am 09:36 AM

How to avoid memory leaks in C# development requires specific code examples. Memory leaks are one of the common problems in the software development process, especially when developing using the C# language. Memory leaks cause applications to take up more and more memory space, eventually causing the program to run slowly or even crash. In order to avoid memory leaks, we need to pay attention to some common problems and take corresponding measures. Timely release of resources In C#, resources must be released in time after use, especially when it involves file operations, database connections, network requests and other resources. Can

C++ compilation error: undefined reference, how to solve it? C++ compilation error: undefined reference, how to solve it? Aug 21, 2023 pm 08:52 PM

C++ is a popular programming language, but during use, the compilation error "undefined reference" often occurs, which brings a lot of trouble to program development. This article will discuss the solution to the "undefined reference" error from both the cause and the solution. 1. Cause of error When the C++ compiler compiles a source file, it will be divided into two stages: the compilation stage and the link stage. The compilation phase converts the source code in the source files into assembly code, while the linking phase combines different source files into an executable file.

Common memory management problems and solutions in C# Common memory management problems and solutions in C# Oct 11, 2023 am 09:21 AM

Common memory management problems and solutions in C#, specific code examples are required. In C# development, memory management is an important issue. Incorrect memory management may lead to memory leaks and performance problems. This article will introduce readers to common memory management problems in C#, provide solutions, and give specific code examples. I hope it can help readers better understand and master memory management technology. The garbage collector does not release resources in time. The garbage collector (GarbageCollector) in C# is responsible for automatically releasing resources and no longer using them.

What is the relationship between memory management techniques and security in Java functions? What is the relationship between memory management techniques and security in Java functions? May 02, 2024 pm 01:06 PM

Memory management in Java involves automatic memory management, using garbage collection and reference counting to allocate, use and reclaim memory. Effective memory management is crucial for security because it prevents buffer overflows, wild pointers, and memory leaks, thereby improving the safety of your program. For example, by properly releasing objects that are no longer needed, you can avoid memory leaks, thereby improving program performance and preventing crashes.

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

How to use C++ reference and pointer parameter passing? How to use C++ reference and pointer parameter passing? Apr 12, 2024 pm 10:21 PM

References and pointers in C++ are both methods of passing function parameters, but there are differences. A reference is an alias for a variable. Modifying the reference will modify the original variable, while the pointer stores the address of the variable. Modifying the pointer value will not modify the original variable. When choosing to use a reference or a pointer, you need to consider factors such as whether the original variable needs to be modified, whether a null value needs to be passed, and performance considerations.

Memory management problems and solutions encountered in Python development Memory management problems and solutions encountered in Python development Oct 09, 2023 pm 09:36 PM

Summary of memory management problems and solutions encountered in Python development: In the Python development process, memory management is an important issue. This article will discuss some common memory management problems and introduce corresponding solutions, including reference counting, garbage collection mechanism, memory allocation, memory leaks, etc. Specific code examples are provided to help readers better understand and deal with these issues. Reference Counting Python uses reference counting to manage memory. Reference counting is a simple and efficient memory management method that records every

See all articles