PHP's garbage collection mechanism-PHP advanced interview questions + detailed explanation

Interview with 10 companies, get 9 offers, PHP interview questions in 2020 zhuanlan.zhihu.com

ps: This article includes selected interview questions and knowledge articles.

PHP Interview Questions About PHP's garbage collection mechanism, PHP's garbage collection mechanism reference counting (reference counting) GC mechanism, PHP can automatically perform memory management, remove unwanted objects, PHP interview questions share PHP's interview questions about garbage collection mechanism:

★My php learning exchange community——856460874. Group management is ready to organize the advanced knowledge system of BAT and other first-line manufacturers (relevant learning materials and written interview questions) welcome to get a promotion together = click to add

Interview questions

  • Introduce PHP's garbage collection mechanism

PHP uses the reference counting (reference counting) GC mechanism, and at the same time uses the root buffer mechanism. When PHP finds that there is a circular reference zval, it will put it into the root buffer. When the root buffer reaches the specified in the configuration file After the number, garbage collection will be performed to solve the memory leak caused by circular references.

  • 1. If the reference count is reduced to zero, the variable container in which it is located will be cleared (free), which is not garbage;
  • 2. If the reference count of a zval is reduced and is still greater than 0, then it will enter the garbage cycle. Secondly, in a garbage cycle, by checking whether the reference count is reduced by 1, and checking which variable containers have zero references to find which part is garbage.

Each object contains a reference counter refcount, each reference is connected to the object, the counter is incremented by 1. When the reference leaves the living space or is set to NULL, the counter is decremented by 1. When the reference counter of an object is zero, PHP knows that you will no longer need to use the object and release the memory space it occupies.

 

  • Which of the following statements about PHP garbage collection is wrong?

A. Turning on/off the garbage collection mechanism can be achieved by modifying the php configuration

B. You can use gc_enable() and gc_disable() to turn on and off in the program.

C. The garbage collection mechanism in PHP will greatly improve system performance.

D. After the garbage collection mechanism is turned on, a lot of memory space can be saved in the case of memory leaks, but because the garbage collection algorithm takes time to run, turning on the garbage collection algorithm will increase the execution time of the script.

Reference answer: C
answer analysis: The garbage collection mechanism in PHP only increases time consumption when the circular collection algorithm is actually running. But in normal (smaller) scripts there should be no performance impact at all.

 

  • What is wrong with the php garbage collection mechanism?

A. In a garbage cycle, by checking whether the reference count is reduced by 1, and checking which variable containers have zero references to find out which part is garbage

B. The garbage collection mechanism can be turned on and off by calling gc_enable() and gc_disable() functions

C. Save memory usage by cleaning up unused variables

D. After the php code is executed, garbage collection will be executed automatically, so there is no need to manually execute garbage collection

Reference answer: D
Answer analysis: A piece of PHP code may take a long time to execute, but if there are unreferenced variables during this period, it will take up memory space and cause problems such as slow operation.

Knowledge

1. Concept

Garbage collection is a memory management mechanism included in most programming languages. Contrary to unmanaged languages: C, C++ and Objective C, users need to manually collect memory. Languages ​​with GC mechanism: Java, javaScript and PHP can automatically manage memory.

Garbage collection mechanism (gc), as the name implies, means waste reuse and is a dynamic storage allocation scheme. It automatically releases allocated memory blocks that are no longer needed by the program. Garbage collection mechanism allows programmers not to care too much about program memory allocation, thus devoting more energy to business logic.

Among the popular languages, the garbage collection mechanism is a common feature of the new generation of languages. For example, Python, PHP, C#, Ruby, etc. all use the garbage collection mechanism.

Two, PHP garbage collection mechanism

1. Before PHP5.3, the garbage collection mechanism used was simply "reference counting".

What is reference counting?
Since PHP is written in C, there is something called a structure in C. Our PHP variables are stored in C in this way.
Every PHP variable exists in a container called zval. A zval container contains not only the variable name and value, but also two bytes of extra information:
● One is called'is_ref', which is a boolean value, used to represent this Whether the variable belongs to the reference collection, through this byte, we can distinguish between ordinary variables and reference variables in PHP.
● The second extra byte is'refcount', which is used to indicate the number of variables pointing to this container.

which is:

① Each memory object is allocated a counter. When the memory object is referenced by a variable, the counter is +1;

② When the variable reference is removed (after unset() is executed), the counter is -1;

③ When the counter=0, it indicates that the memory object is not used, the memory object is destroyed, and the garbage collection is completed.

 

And PHP will release the content occupied by this process/thread after a life cycle ends. This method determines that PHP does not need to consider memory leaks in the early stage.

But when two or more objects refer to each other to form a ring, the counter of the memory object will not be reduced to 0; at this time, this group of memory objects is useless, but cannot be recycled, which leads to memory leaks.

Beginning with php5.3, a new garbage collection mechanism was used, and a complex algorithm was implemented based on reference counting to detect the existence of reference loops in memory objects to avoid memory leaks.

 

  • 2. With the development of PHP, the increase of PHP developers and the expansion of its business scope, a more complete garbage collection mechanism was introduced in PHP5.3, and the new garbage collection mechanism solved the inability to deal with circular reference memory. Leakage problem.

As the official document says: every php variable is stored in a variable container called "zval". A zval variable container, in addition to containing the type and value of the variable, also includes two bytes of additional information. The first one is "is_ref", which is a bool value, which is used to identify whether this variable belongs to a reference set. Through this byte, the php engine can distinguish between ordinary variables and reference variables. Since php allows users to use custom references 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 called symbols) that point to this zval variable container. All symbols are stored in a symbol table, where each symbol has a scope (scope).

The official document says that you can use Xdebug to check the reference count:

<?php
$a = "new string";
$c = $b = $a;
xdebug_debug_zval( 'a' );
unset( $b, $c );
xdebug_debug_zval( 'a' );
?>

 

The above routine will output:

a: (refcount=3, is_ref=0)='new string'
a: (refcount=1, is_ref=0)='new string'

Note: Starting from the NTS version of PHP7, the references of the above routines will no longer be counted, that is, the reference count of a after $c=$b=$a is also 1. The specific classification is as follows:

In PHP 7, zval can be reference counted or unquoted. There is a flag in the zval structure to determine this.

① For null, bool, int and double type variables, refcount will never count;

② For objects and resource types, the refcount count is the same as that of php5;

③ For character strings, unquoted variables are called "actual strings". Those quoted strings are repeatedly deleted (that is, there is only one inserted string with specific content) and guaranteed to exist for the entire duration of the request, so there is no need to use reference counting for them; if opcache is used, These strings will exist in shared memory. In this case, you cannot use reference counting (because our reference counting mechanism is non-atomic);

④For arrays, unreferenced variables are called "immutable arrays". The count of the array itself is consistent with php5, but the count of each key-value pair in the array is based on the previous three rules (that is, if it is a string, it will not count); if you use opcache, the constant array text in the code will be Convert to an immutable array.

 

Again, these live in shared memory, so refcounting cannot be used.

Our demo example is as follows:

<?php
echo '测试字符串引用计数';
$a = "new string";
$b = $a;
xdebug_debug_zval( 'a' );
unset( $b);
xdebug_debug_zval( 'a' );
$b = &$a;
xdebug_debug_zval( 'a' );
echo '测试数组引用计数';
$c = array('a','b');
xdebug_debug_zval( 'c' );
$d = $c;
xdebug_debug_zval( 'c' );
$c[2]='c';
xdebug_debug_zval( 'c' );
echo '测试int型计数';
$e = 1;
xdebug_debug_zval( 'e' );

The output you see is as follows:

3. Payback cycle

By default, PHP's garbage collection mechanism is turned on, and there is a php.ini setting that allows you to modify it: zend.enable_gc.

When the garbage collection mechanism is turned on, the algorithm will determine that whenever the root buffer is full, it will perform a circular search. The root buffer has a fixed size, the default is 10,000. You can modify this value by modifying the constant GC_ROOT_BUFFER_MAX_ENTRIES in the PHP source file Zend/zend_gc.c, and then recompiling PHP. When the garbage collection mechanism is closed, the circular search algorithm will never be executed, however, the root will always be stored in the root buffer, regardless of whether the garbage collection mechanism is activated in the configuration.

In addition to modifying the configuration zend.enable_gc, the garbage collection mechanism can also be turned on and off by calling the gc_enable() and gc_disable() functions respectively when running php. Calling these functions has the same effect as modifying configuration items to turn on or off the garbage collection mechanism. Even when it is possible that the root buffer is not full, it is possible to force periodic recycling. You can call the gc_collect_cycles() function for this purpose. This function will return the number of cycles recovered using this algorithm.

The reason for allowing the garbage collection mechanism to be turned on and off and for autonomous initialization is because certain parts of your application may be time-sensitive. In this case, you may not want to use garbage collection. Of course, turning off the garbage collection mechanism for certain parts of your application is at the risk of possible memory leaks, because some roots may not be stored in the limited root buffer.

Therefore, it may be wise to call gc_collect_cycles() just before you call gc_disable() to release the memory. Because this will clear all possible roots that have been stored in the root buffer, and then when the garbage collection mechanism is closed, an empty buffer can be left to have more space to store the possible roots.

Fourth, performance impact

1. Saving of memory footprint

First of all, the whole reason for implementing the garbage collection mechanism is to save memory usage by cleaning up circularly referenced variables once the prerequisites are met. In PHP execution, once the root buffer is full or the gc_collect_cycles() function is called, garbage collection will be performed.

2. Increased execution time

The second area where garbage collection affects performance is the time it takes to release leaked memory.

Generally, the garbage collection mechanism in PHP only increases time consumption when the circular collection algorithm is actually running. But in normal (smaller) scripts there should be no performance impact at all.

3. In the case of normal scripts running with a recycling mechanism, memory savings will allow more such scripts to run on your server at the same time. Because the total memory used has not reached the upper limit.

This benefit is especially obvious in long-running scripts, such as long-term test suites or daemon scripts. At the same time, for script applications that usually run longer than Web scripts, the new garbage collection mechanism should greatly change the perception that memory leaks are difficult to solve.

Finally, I wish all of you a success in the interview and get your favorite offer.

I have compiled some information on this, including but not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell Scripts, Docker, microservices, Nginx and other knowledge points can be shared with you for free.

If you want to communicate and learn with a group of 3-8 years of senior developers, you need my official group- click here .

: Tencent T3-T4 standard boutique PHP architect tutorial catalogue, as long as you finish reading it, the salary will rise one step (continuous update)​

Guess you like

Origin blog.csdn.net/weixin_43814458/article/details/107851427