php memory management

The PHP memory management implementation: When the variable assignment is not passed directly deep copy, but a number of variables share the same value, the reference count value to record the number of variables and then use, when a variable is changed, this time can not continue and when the common variable value before, this time will be a deep copy, separation value, copying is written this time.

eg:

$a = array();//$a refcount=1

$b = $a;//$a $b refcount=2

$c = $b;//$a $b $c refcouny=3

unset($b) //$a $c refcount=2

As can be seen from the above when the data is not copied and assigned PHP, but refcount value plus one;

Reference count: Record the number of zval execution zend_value, when there is a new zval refcount is increased by one point to it, when the refcount is 0, that value has not been directed, this is the value would be released. ( Not all value type structure reference count will be used, such as plastic, float, boolean, null. Zval save them directly, so these types do not share value, but a deep copy, which is unique to PHP7 , doing so for some small memory data eliminates the process of counting, is to PHP7 data storage can refer https://www.jb51.net/article/148865.htm ) there is a special situation does not uses reference counting:

$a = 'aa';

$b = $a;

Above this reference count will not be used because $ a is a unique constant value, the life cycle of these strings will be destroyed after the release of unity during the entire execution request, request to complete, so no need to refer to during operation count. Another variable is not set.

$a = 'aa'.time();

$ B = $ a; // will use this reference count. refcount = 2

Which set of data shows:

var_dump (memory_get_usage ()); // get memory method, coupled with true return the actual memory, without memory performance returns
$a = 324324;
var_dump(memory_get_usage());
unset($a);
var_dump(memory_get_usage());
int (349016) int (349016) int (349016) // $ a is the same regardless of where the (relatively small) string, integer, float it will not be subject to unset affected, because its value is the presence of zend_value in, thinking reference: https://www.jb51.net/article/148865.htm
var_dump (memory_get_usage ()); // get memory method, coupled with true return the actual memory, without memory performance returns
$a = time().'hazel';
$b = $a;
$c = time().'hazel2';
var_dump(memory_get_usage());
unset ($ a); // When we get rid of this paragraph may find the value is unchanged, because he is the reference count will not delete a reduction in memory until $ a and $ b are deleted.
unset($c);
var_dump(memory_get_usage());
int(349024) int(349112) int(349064)

Copy On Write:

$a = 10;//将常量值赋给变量,会为a分配内存空间

$b $a;//变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢?

$c = &$a;//引用是不会为c分配空间的,c和a是共用一份空间的。

$a = 5;

echo $c;//输出5,因为a和c 是指向同一个内存空间

echo PHP_EOL;

echo $b;//由于b是副本,对a的操作不会影响b,输出10

最后 refcount=2  is_ref_gc=1  

回收机制:

Recovery mechanisms There are two cases:

1、refcount=0;

  • zval a depth-first traversal algorithm can traverse to traverse all zval buffer for each root root, and each of the zval refcount is decremented by 1, while the same in order to avoid multiple zval minus 1 (because the roots can be different traverse to the same zval), decrements by one after a zval of its marked as "minus."
  • Again zval first traversal of the root depth of each buffer, if a zval the refcount is not 0, then add 1 to its otherwise maintain their zero.
  • Clear all root root buffer (note that these zval removed from the buffer rather than destroy them), and then destroy all refcount is zval 0, and to recover its memory.

2, there is a for arrays and objects: refcount if a value after reduction also greater than zero, then the garbage collector will collect it as a garbage value, and so on up to a certain number of open garbage identification procedures, the real garbage released.

Collection algorithm:

Reference: https://www.cnblogs.com/lovehappying/p/3679356.html

Guess you like

Origin blog.csdn.net/qq_38234594/article/details/88369748