Home>Article>Backend Development> Selected 3 interview questions to help you understand PHP's garbage collection mechanism! !

Selected 3 interview questions to help you understand PHP's garbage collection mechanism! !

青灯夜游
青灯夜游 forward
2021-06-04 19:25:48 3266browse

This article will share with you advanced interview questions about the PHP garbage collection mechanism, and give you an in-depth understanding of the PHP garbage collection mechanism. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Selected 3 interview questions to help you understand PHP's garbage collection mechanism! !

#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 manage memory and clear unnecessary objects, PHP interview questions sharing PHP interview questions about the garbage collection mechanism:

Recommended study: "PHP Video Tutorial"

Interview Questions


Introduce PHP’s garbage collection mechanism

PHP uses reference counting (reference counting) GC mechanism, and also uses the root buffer mechanism. When PHP finds that there is a zval with a circular reference, it will put it into the root buffer. When the root buffer reaches the specified number in the configuration file, it will garbage Recycling to solve the memory leak problem caused by circular references.

  • 1. If the reference count is reduced to zero, the variable container will be cleared (free) and 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, during a garbage cycle, find out which parts are garbage by checking whether the reference count is reduced by 1 and checking which variable containers have zero references.

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

Which of the following statements about PHP garbage collection is incorrect?

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

B. It can be turned on and off using gc_enable() and gc_disable() in the program.

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

D. After turning on the garbage collection mechanism, a large amount of memory space can be saved in case of memory leaks. However, since 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 will only increase the time consumption when the recycling algorithm is actually running. But in normal (smaller) scripts there should be no performance impact at all.

#Which statement about the php garbage collection mechanism is wrong?

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

B. You can turn on and off the garbage collection mechanism by calling the gc_enable() and gc_disable() functions

C. Save memory usage by cleaning up unused variables

D, php Garbage collection will be automatically performed after the code is executed, so there is no need to manually perform 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 occupy memory space and cause problems such as slow operation

Knowledge Chapter


1. Concept

Garbage collection is a feature that is included in most programming languages. memory management mechanism. In contrast to unmanaged languages: C, C and Objective C, where users need to manually collect memory, languages with GC mechanisms: Java, javaScript and PHP can automatically manage memory.

Garbage collection mechanism (gc), as the name suggests, means waste reuse. It is a dynamic storage allocation scheme. It automatically releases allocated memory blocks that are no longer needed by the program. 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.

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

2. PHP garbage collection mechanism

1. Before PHP5.3 version, the garbage collection mechanism used was a simple "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 this way in C.
Each PHP variable exists in a container called zval. A zval container, in addition to the variable name and value, also includes two bytes of additional information:
● One is called 'is_ref', which is a Boolean value used to indicate whether this variable belongs to the reference set. Through this byte, we can distinguish ordinary variables from reference variables in PHP.
● The second extra byte is 'refcount', which is used to indicate the number of variables pointing to this container.

That is:

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

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

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

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 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 no longer useful, but cannot be recycled, so causing memory leaks.

Starting from php5.3, a new garbage collection mechanism has been used. Based on reference counting, a complex algorithm has been implemented to detect the existence of reference rings in memory objects to avoid memory leaks.

  • 2. With the development of PHP, the increase of PHP developers and the expansion of the business scope it carries, a more complete garbage collection mechanism and new garbage collection have been introduced in PHP5.3 The mechanism solves the problem of reference memory leaks that cannot handle cycles.

As the official documentation says: 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, and each symbol has a scope.

Official documents say that you can use Xdebug to check the reference count:

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 above References to the routine will no longer be counted, i.e. c = c= c=b=The reference count of a after $a is also 1. Specifically The classification is as follows:

In PHP 7, zvals can be reference counted or not. There is a flag in the zval structure that determines this.

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

② For objects and resource types, refcount count is consistent with php5;

③ For strings, unquoted variables are called "actual strings". Those referenced strings are deduplicated (i.e. 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 live in shared memory, in which case you cannot use reference counting (because our reference counting mechanism is non-atomic);

④For arrays, unreferenced variables are called "Immutable array". 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 is not counted); if opcache is used, the constant array literal in the code will be Convert to immutable array.

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

Our demo example is as follows:


      

The output you see is as follows:

Selected 3 interview questions to help you understand PHPs garbage collection mechanism! !

3. Recycling 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, a loop search will be performed. The root cache area has a fixed size, with a default size of 10,000. This value can be modified by modifying the constant GC_ROOT_BUFFER_MAX_ENTRIES in the PHP source code file Zend/zend_gc.c and then recompiling PHP. When garbage collection is turned off, the loop search algorithm is never executed, however, the root will always exist in the root buffer, regardless of whether garbage collection is activated in the configuration.

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

The reason you allow turning garbage collection on and off and allowing autonomous initialization is because some parts of your application may be time-sensitive. In this case, you probably don't want to use garbage collection. Of course, turning off garbage collection for certain parts of your application runs the risk of possible memory leaks, since some possible roots may not fit into the limited root buffer.

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

4. Performance impact

1. Saving of memory space

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

2. Increased execution time

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

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

3. When there is a recycling mechanism running in ordinary scripts, 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-running test suites or daemon scripts. At the same time, for script applications that typically run longer than Web scripts, the new garbage collection mechanism should greatly change the long-held view that memory leaks are difficult to solve.

Finally, I wish all of you can pass the interview and get your favorite offer.

For more programming-related knowledge, please visit:Introduction to Programming! !

The above is the detailed content of Selected 3 interview questions to help you understand PHP's garbage collection mechanism! !. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete