PHP thread memory recycling problem, php thread recycling
When a PHP thread ends, all the memory space currently occupied will be destroyed. So if this thread does not end, how can we recycle the memory?
Refcount: refers to the number of pointers pointing to the container.
Is_ref: whether it is referenced (only 0 or 1)
Assignment process:
<? Php $ a = 'a'; xdebug_debug_zval (a); // (refcount = 1, is_ref = 0), string 'aa' (length = 6) $ B = $; // The following two are actually a variable container xdebug_debug_zval (a); // (refcount = 2, is_ref = 0), string 'aa' (length = 6) xdebug_debug_zval (B); // (refcount = 2, is_ref = 0), string 'A' (length = 6) unset ($ B ); // subtract 1xdebug_debug_zval (a) from the variable container refcount; // (refcount = 1, is_ref = 0), string 'A' (length = 6) xdebug_debug_zval (B ); // B: no such symbol B variable is destroyed, pointing to disconnected. If the reference technology of the corresponding container is zero, the memory of this block is recycled $ B = $; $ B = 'bb'; xdebug_debug_zval (a); // (refcount = 1, is_ref = 0), string 'A' (length = 6) xdebug_debug_zval (B ); // (refcount = 1, is_ref = 0), string 'A' (length = 6) re-apply for a variable container to store B, a variable container reference minus 1
Reference process:
<? Php $ a = 'a'; xdebug_debug_zval ('A'); // (refcount = 1, is_ref = 0), string 'aa' (length = 2) $ B = & $ a; // variable container's reference technology plus 1. The reference tag is set to 1xdebug_debug_zval ('A'); // (refcount = 2, is_ref = 1 ), string 'A' (length = 2) xdebug_debug_zval ('B'); // (refcount = 2, is_ref = 1), string 'aa' (length = 2) $ B = '000000'; // php will find that the container variable is referenced (is_ref), so the container variable does not need to apply for an xdebug_debug_zval ('A') as it is assigned a value '); // (refcount = 2, is_ref = 1), string '000000' (length = 2) xdebug_debug_zval ('B'); // (refcount = 2, is_ref = 1 ), string '200' (length = 2) unset ($ B); // variable container application count minus 1, reference to zero xdebug_debug_zval ('A'); // (refcount = 1, is_ref = 0), string '000000' (length = 2) xdebug_debug_zval ('B'); // B: no such symbol
If one is unset after multiple references, will the is_ref be set to zero? Variable containers are still referenced. Let's take a look:
<? Php $ a = 'a'; $ B = & $ a; $ c = & $ a; // you can see that the reference refCount is 3, is_ref is always 1xdebug_debug_zval ('A'); // (refcount = 3, is_ref = 1), string 'A' (length = 2) xdebug_debug_zval ('B '); // (refcount = 3, is_ref = 1), string 'A' (length = 2) xdebug_debug_zval ('C'); // (refcount = 3, is_ref = 1 ), string 'A' (length = 2) unset ($ B); // The expected bug does not appear, but the refcount minus 1, is_ref or 1xdebug_debug_zval ('A '); // (refcount = 2, is_ref = 1), string 'A' (length = 2) xdebug_debug_zval ('B'); // B: no such symbolxdebug_debug_zval ('C'); // (refcount = 2, is_ref = 1), string 'aa' (length = 2) // how does php know that this container has references? After all, is_ref is still 1 and cannot be counted. Now refcount takes effect. It tells php that the variable has several references, but the problem arises again. If I do something bad and assign a value when I reference it, will it have a bug $ e = $ a; // we can see that the expected bug still does not appear, at this time, the value is not as simple as the direct value assignment refcount plus 1, but a new variable container xdebug_debug_zval ('A') is applied; // (refcount = 2, is_ref = 1), string 'A' (length = 2) xdebug_debug_zval ('E'); // (refcount = 1, is_ref = 0 ), string 'A' (length = 2)
Can unset and null values be used to reclaim variables? Many people mistakenly think that both of them can recycle the variable space. In fact, the error is that null only reduces the space occupied by the variable. In terms of collection, the container still exists.
<? Php $ a = 'a'; $ B = $ a; $ B = null; // another variable container xdebug_debug_zval ('A') is applied '); // (refcount = 1, is_ref = 0), string 'A' (length = 2) xdebug_debug_zval ('B'); // (refcount = 1, is_ref = 0 ), null variable space is not recycled unset ($ B); // at this time, the space of the B Variable container is released xdebug_debug_zval ('A'); // (refcount = 1, is_ref = 0), string 'A' (length = 2) xdebug_debug_zval ('B'); // B: no such symbol
Summary
1. Garbage Collection Time
In PHP, if the reference count is 0, the memory is immediately released. That is to say, there is no variable referenced in the loop, and the memory is immediately released out of the scope of the variable. The ring reference detection is triggered when certain conditions are met. Therefore, in the above example, we can see that the memory usage fluctuates greatly. You can also use the gc_collect_cycles function to actively perform ring reference detection.
2. & Symbol impact
Explicitly referencing a variable increases the reference count of the memory:
$ A = "something ";
$ B = & $;
At this time, unset ($ a), but there is still a reference of $ B pointing to the memory area, the memory will not be released.
3. Impact of unset Functions
Unset only disconnects a variable from A memory area, and calculates the reference count of the memory area-1. In the preceding example, inside the loop body, $ a = new (); unset ($ a); does not reduce the reference count of $ a to zero;
4. = effect of the null operation;
$ A = null: directly empty the data structure pointed to by $ a and return the reference count to 0.
5. Impact of script execution completion
After the script is executed, all memory used in the script will be released, regardless of whether there is a reference ring.