Home > Backend Development > PHP Tutorial > PHP kernel - graphic introduction to memory leaks and new garbage collection mechanism

PHP kernel - graphic introduction to memory leaks and new garbage collection mechanism

黄舟
Release: 2023-03-06 13:24:01
Original
2036 people have browsed it

The garbage collection mechanism is a dynamic storage allocation scheme. It automatically releases allocated memory blocks that are no longer needed by the program. The process of automatically reclaiming memory is called garbage collection. The garbage collection mechanism allows programmers not to worry too much about program memory allocation, so that they can devote more energy to business logic.

PHP also implements dynamic management of memory at the language layer. This has been explained in detail in the previous chapters. Dynamic management of memory frees developers from cumbersome memory management. Come to the rescue. In conjunction with this, PHP also provides a garbage collection mechanism at the language layer, so that programmers do not have to worry too much about program memory allocation.

Before PHP5.3, PHP only had simple garbage collection based on reference counting, when a variable is referenced When the count becomes 0, PHP will destroy this variable in memory, but the garbage here cannot be called garbage. And PHP will release the content occupied by this process/thread after the end of a life cycle. This method determines that PHP does not need to consider too much memory leakage in the early stage. However, with the development of PHP, the number of PHP developers has increased and the business scope it carries has expanded.

A more complete garbage collection mechanism was introduced in PHP5.3. The new garbage collection mechanism solves the problem of reference memory leaks that cannot handle cycles. Next, we will discuss the new garbage collection mechanism with example test data.



##(tips: What are refcount and zval?

Each variable is stored in a zval structure at the bottom of PHP. Variables with the same value share a value. Use refcount to save the number of variables pointing to this value.

For example, $a =$b=1, then they point to a zval with a value of 1 and refount=2, which means there are two variables pointing to it. For details, see PHP kernel storage mechanism (separation/change ))

The following is the actual data A test, the conclusion is in the lower right corner:




##How to avoid memory leaks:

1. Write High-quality code to reduce the possibility of memory leaks

2. Recycle according to the PHP life cycle and let the system automatically recycle memory (effective for non-resident programs, RINIT, RSHOTDOWN)

3. Manual recycling, that is, restart the server nginx or apache (MINIT,MSHOTDOWN)

4. Increase the php.ini configuration file: php.ini memory_limit = 128M (not recommended)

##Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 38218371 bytes) 128MB = 134217728byte


The above is the detailed content of PHP kernel - graphic introduction to memory leaks and new garbage collection mechanism. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template