


What is garbage collection in PHP? What impact does it have on performance?
This article will introduce you to garbage collection, briefly analyze the garbage collection mechanism in PHP, and finally talk about the impact of garbage collection on performance. Let’s take a look!
# I believe that students who have learned a little bit about development know that regardless of any programming language, a variable will be stored in memory. In fact, we developers are constantly manipulating memory back and forth. Correspondingly, if we keep adding new variables, the memory will keep increasing. If there is no good mechanism, then the memory will increase without limit and eventually fill up. All memory. This causes a memory leak. But in daily development, unless a large file is loaded at a time, we almost never see memory overrun errors. This is the role of the garbage collection mechanism.
What is garbage collection?
When using C language, we have to manually use free to release memory. Most programming languages after C will have their own processing capabilities such as garbage collection, which is what we have today The garbage collection mechanism we are talking about is also called GC. In development languages with GC capabilities, we do not need to care about when to release memory, and we do not even need to understand the content of this piece at all, because these languages have already handled the issue of memory release for us at the bottom level.
Of course, the most famous content in this area is the garbage collection mechanism in Java. In fact, PHP also has a corresponding processing mechanism. Of course, many PHPers may have never been exposed to it. Today we will discuss this aspect. .
PHP’s garbage collection algorithm
In the previous article, we introduced the concept of reference counting. Before PHP5.3, PHP's garbage collection mechanism was very simple, which was to clean up and recycle all items with a refcount of 0, which was free at the bottom level. But this method will bring about a problem, which is the circular reference we talked about in the article on reference counting. This kind of reference problem cannot be recycled through ordinary judgment of refcount. So before PHP5.3, circular references would cause memory leaks.
The reason why I emphasize the version is because after 5.3, PHP improved the garbage collection algorithm and solved this circular reference. (Of course, we try to avoid this kind of circular reference problem in daily development). For the specific algorithm, we quote the official picture:
There is a detailed explanation in the official document, but it is still very confusing. Let’s use simple language (human language) to describe this process.
First of all, we have the concept of root buffer, which is the root in the picture. At the bottom level, through a series of incomprehensible algorithms, we can find a possible root of each variable. PHP will put the possible roots of the variable into the root buffer.
When the root buffer is full, the default value is generally 10000. You need to modify the source code and recompile to modify this value. PHP will start the garbage collection mechanism, follow the depth traversal algorithm from the root buffer to find all the variables related to this possible root, and reduce the refcount of the variable found by a certain possible root by 1, and make a mark to mark the current one. "Reduced".
Then traverse deeply again. If refcount is not 0, add 1. If it is 0, keep it unchanged.
Then clear all possible roots in the root buffer, clearing rather than deleting. Then clean and release all variable contents with refcount of 0.
Are you confused already? In fact, I am also very confused. I don’t even know how this paragraph was written....
Just remember a few key points and you can handle the interview and kill most people instantly.
- After PHP5.3, we do not directly check whether the refcount of each variable is 0.
- The algorithm used is depth traversal. There is a root buffer and it is cleaned according to it. The specific algorithm requires a solid C and algorithm foundation, so study it carefully when learning the source code
- 5.3 Later, the algorithm solved the problem of circular references
- The memory leak value will remain within a certain range , there will be no immediate large-scale crash
The impact of garbage collection on performance
As mentioned earlier, garbage collection will occur after the root buffer is full It will be executed immediately. There will also be two depth traversals, which inevitably leads to performance consumption. After all, the execution of algorithms takes time. However, compared with devastating errors such as memory overflow, the performance loss caused by garbage collection is basically negligible.
Summary
In fact, we only need to remember a few key points about garbage collection. The specific core algorithm and content need to be studied in more depth. You can only fully understand it after reading the source code. Of course, this is also the goal of our study. We will definitely get involved in the underlying content of the source code in the future, so let us wait and see!
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is garbage collection in PHP? What impact does it have on performance?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
