PHP Memory Management Write-time copy garbage collection

Source: Internet
Author: User

Variables in PHP do not need to be released manually, the kernel helps us implement variable memory management, including memory allocation and recycling

The problem with deep copy of variables is that efficiency and memory waste are serious.

Resolve deep Copy: 1, reference count 2, copy on write

The memory management of PHP variables is based on the two-point implementation

When a variable is assigned or referenced, it is not a deep copy, but multiple variables share a value, the reference count is used to record how many times the variable has been referenced, and when one of the variables changes, it cannot be shared with other variables.

Value, this time a deep copy is required to detach value, which is the copy on write.

Reference count:

Used to record how many zval are currently pointing to the same zend_value, when there is a new zval pointing to this value, counter +1, when the Zval is destroyed, the Count book-1, when the counter for 0,value is released.

The reference count for PHP7 is saved in Zend_value, and the member GC is used to hold the reference count. The local variable Zavl in PHP is allocated on the ZEND_EXECUTE_DATA structure, which is the EXECUTE_EX function we are debugging.

The Execute_data variable, which is the most important and critical structure during the run, is used for assigning local variables, saving execution locations, invoking context switches, and so on.

Copy on write:

It only makes deep copy when it is necessary to write, can improve efficiency, such as the Linux operating system Fork sub-process will not immediately replicate the address space of the parent process, but let the parent-child process to share a memory space, only when the need to write the address space will be copied, This allows each process to have its own separate address space, meaning that the replication of the resource is done only when it is required to be written, and previously shared in read-only mode. Not all types can be replicated, and object resources cannot replicate.

Automatic GC Recovery:

The execution of the Zval break value is if it is found that refounct=0 will directly release value, this is the time when the variable is recycled, two common cases of disconnection occur when the change variable and function return, modify the variable is to break the value of the pointer, the function returns will release all the variables. You can also actively destroy a variable using the unset () function

Garbage collection:

The automatic GC mechanism of a variable is implemented by reference counting, but there is a case where the mechanism cannot be resolved, so that the memory is never released due to the variable's inability to reclaim, causing a memory leak. This is a circular reference, and a circular reference is a reference to itself that cannot be freed, such as an array in which an element references the array. Because such variables, which are not released because of circular references, are called Garbage, PHP refers to another mechanism to recycle the garbage, which is the garbage collector:

Here to be clear, if a variable value of refcount is reduced to 0,value release, it does not belong to garbage. If the refcount of a variable is greater than 0, then it may be garbage, the garbage collector will collect this variable, the garbage collector will save the garbage to a buffer buffer, and so on a certain amount will start the Garbage authentication recovery program, recovery algorithm principle is actually very simple, Since the garbage collection is caused by the reference itself Ah, then I will be the value of the refcount minus 1, if the return is 0 is garbage, otherwise the reduction of 1 recovery, the description is a normal variable.

PHP Memory Management Write-time copy garbage collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.