PHP garbage collection mechanism----copy and reference count at write time

Source: Internet
Author: User
Tags variable scope

PHP uses reference counts and write-time replication to manage memory. Copy-on-write ensures that the copied values between variables do not waste memory, and the reference count guarantees that the memory is freed to the operating system when the variable is no longer needed.

To understand PHP memory management, first understand a concept----symbol table.

  Concept of symbol table:

A variable consists of two parts: a variable name and a variable value. The symbol table is the array that maps the variable name to the address of the variable value in memory.

  Copy on write:

When the value of one variable is copied to another variable, PHP does not use more memory for the copied value. Instead, he will follow the new symbol table to show that the two variables have the same block of memory. So the following code does not actually create a new array:

<?php

$people = Array ("Alice", "Bob");

$other = $people; Array has not been copied

?>

If you modify any of the replicas, PHP allocates the required memory for replication:

$people [1] = "Tom"; The value changes, the array is copied, the new memory is allocated

With deferred allocation and replication, PHP saves time and memory in many cases, which is copy-on-write.

  Reference count:

There's not a single job in the symbol table. A reference counter, whose value represents the number of ways to get that piece of memory.

After assigning values to $people and $other, the array points to the symbol table, and the value of the $people and $other reference counters is 2. In other words, there are two ways to get that piece of memory: $people and $other.

However, when $people[1] is changed only, PHP creates a new array for $people, $people and $ The value of the reference counter for other is 1. When a variable leaves the scope, such as function arguments and local variables, the value of the counter is subtracted by 1 when it reaches the end of the function. When a variable is assigned a value in another memory space, the old worthy reference count is reduced by 1. When the value of the reference count is 0 o'clock, his memory is freed.

This is the reference count.

Reference counting is the preferred way to manage memory. The hold variable scope is limited to the function, passed by value, and the reference count is responsible for memory management. You can use the Isset () and unset () functions if you want to take the initiative to get more information or control to release the value of a variable.

PHP garbage collection mechanism----copy and reference count at write time

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.