Implementation instructions
1. Add a reference counter to the object. The value of the reference counter will increase every time somewhere. Whenever a reference becomes invalid, the counter is decremented by one.
If the refcount of the variable value is reduced by one and equals 0, the value will be released and is not garbage. The garbage collector does not handle it.
If the refcount of the variable value is greater than 0 after being reduced by one, the value is considered to be unreleasable and may become garbage.
2. The garbage collector collects possible garbage. After reaching a certain amount, it starts the garbage identification program and releases the real garbage.
Example
<?php // PHP garbage collection mechanism case: refer to the php manual //--------------------Scalar type-------------------- // tip: Each PHP variable exists in a variable container called "zval", which contains the type and value of the variable, "is_ref": whether it is a reference variable, "refcount": reference count // ex: Generate a new zval container $a = 'new string'; // ex: display zval container information xdebug_debug_zval('a'); // a: (refcount=1, is_ref=0),string 'new string' (length=10) // ex: Increase the reference count of the zval container $c = $b = $a; xdebug_debug_zval('a'); // a:(refcount=3, is_ref=0),string 'new string' (length=10) xdebug_debug_zval('b'); // b:(refcount=3, is_ref=0),string 'new string' (length=10) xdebug_debug_zval('c'); // c:(refcount=3, is_ref=0),string 'new string' (length=10) // tip: There is only one container at this time, because PHP will not copy the generated variable container when it is not necessary // At this time, this variable container is associated with variable a, variable b and variable c. unset($b); // ex: reduce reference count xdebug_debug_zval('a'); // a:(refcount=2, is_ref=0),string 'new string' (length=10) // tip: unset When deleting a variable, the refcount variable count is decremented by one. At this time, only $a and $b point to the variable container. unset($a); unset($c); var_dump($a); // tip: At this time, recount is 0 and the variable is deleted // When recount becomes 0, the variable container containing the type and value will be deleted from the memory. //--------------------Composite type------------- echo '--------------Composite type------------<br/>'; $a = array( 'name' => 'junior', 'age' => 18 ); xdebug_debug_zval('a'); // a:(refcount=1, is_ref=0), // array (size=2) // 'name' => (refcount=1, is_ref=0),string 'junior' (length=6) // 'age' => (refcount=1, is_ref=0),int 18 // ex: Add an existing element to the array $a['love'] = $a['name']; xdebug_debug_zval('a'); // a:(refcount=1, is_ref=0), // array (size=3) // 'name' => (refcount=2, is_ref=0),string 'junior' (length=6) // 'age' => (refcount=1, is_ref=0),int 18 // 'love' => (refcount=2, is_ref=0),string 'junior' (length=6) // $a = array('one'); // xdebug_debug_zval('a'); // // $b = &$a; // $c = $a; // $b = &$c; // xdebug_debug_zval('b'); // xdebug_debug_zval('c'); // xdebug_debug_zval('a'); // Clean up the variable container problem echo '------------Memory leak problem-----------<br/>'; $a = array('one'); xdebug_debug_zval('a'); // a:(refcount=1, is_ref=0), // array (size=1) // 0 => (refcount=1, is_ref=0),string 'one' (length=3) $a[] = &$a; xdebug_debug_zval('a'); // a:(refcount=2, is_ref=1), // array (size=2) // 0 => (refcount=1, is_ref=0),string 'one' (length=3) // 1 => (refcount=2, is_ref=1), // &array // unset($a); // (refcount=1, is_ref=1)=array ( // 0 => (refcount=1, is_ref=0)='one', // 1 => (refcount=1, is_ref=1)=... // ) // tip: After unset($a), the reference count is decremented by one, even though there is no longer any symbol in a scope pointing to this structure (that is, the variable container), // Since array element "1" still points to the array itself, this container cannot be cleared // Since there is no other symbol pointing to it, the user has no way to clear this structure, resulting in a memory leak // Fortunately, PHP will This data structure is cleared at the end of script execution, but a lot of memory will be consumed before PHP clears it. // The same thing happens with objects, in fact it's more likely to happen with objects because objects are always implicitly referenced.
The above is how PHP reference counting implements garbage collection. I hope it will be helpful to everyone.