PHP Note variables and memory management

Source: Internet
Author: User

Brother Bird's blog is a big treasure, is looking at from the beginning.


"In-depth understanding of PHP memory management who moved my memory"

Http://www.laruence.com/2011/03/04/1894.html


1. Memory_get_usage function

int Memory_get_usage ([bool $real _usage = false])


Memory used by PHP scripts (without the memory of the Memory_get_usage () function itself)

Memory_get_usage () parameter $real_usage, default is FALSE, and when set to TRUE, the resulting memory value is the memory occupied by PHP memory management.

PHP allocated to the memory Var_dump (Memory_get_usage (true));//php uses internal memory var_dump (Memory_get_usage ());

Results

int 262144
int 239368


PHP's memory management mechanism is: pre-allocated a piece of space, used to store variables, when the space is not enough, then apply for a new space.

Memory_get_usage (True) is the pre-allocated memory.


I didn't pay attention to the memory usage of the PHP page before, and we should use this function to analyze the code after the page optimization.


2. Variable assignment and memory

Var_dump (Memory_get_usage ()); $a = "Hello world"; Var_dump (Memory_get_usage ()); $b = $a; Var_dump (Memory_get_usage ()); Unset ($b); Var_dump (Memory_get_usage ()); unset ($a); Var_dump (Memory_get_usage ());


Results

int 239776
int 239912
int 240000
int 239912
int 239776


239912-239776=136

240000-239912=88


The amount of memory used by the $a and $b assignment is different because the $b assignment does not allocate memory, which is consumed by the output function. 88 bytes.

In addition, destroying $b does not affect the memory used by $ A, which is why memory management reference counts

Changing the example will give you a clear understanding of the relationship between variable assignment and memory.


Var_dump (Memory_get_usage ()); $a = "Hello World", Var_dump (Memory_get_usage ()); $b = "Hello World"; Var_dump (memory_get_ Usage ()); unset ($b); Var_dump (Memory_get_usage ()); unset ($a); Var_dump (Memory_get_usage ());

Results

int 239816
int 239952
int 240088
int 239952
int 239816

239952-239816=136

240088-239952=136


In addition, reference assignment takes on memory rules and is similar to direct assignment, which also involves reference counting rules for memory management


3, PHP garbage collection mechanism, this part of the understanding is not deep enough, simply record a function

Xdebug_debug_zval need to install xdebug extension

$a = "Hello world"; Xdebug_debug_zval (' a ');

Output

A:

(Refcount=1, is_ref=0), String ' Hello world ' (length=11)

RefCount number of variables pointing to this value

Is_ref represents whether there is an address reference

Type

Value

Variable assignment: Is_ref is false refcount is 1

Manual Address

Basic knowledge of reference counting

http://php.net/manual/zh/features.gc.refcounting-basics.php

Each PHP variable exists in a variable container called "Zval". A Zval variable container that includes two bytes of extra information in addition to the type and value of the variable. The first is "Is_ref", which is a bool value that identifies whether the variable belongs to a reference collection (reference set). With this byte, the PHP engine can differentiate between normal and reference variables, and since PHP allows users to use the custom reference by using &, there is an internal reference counting mechanism in the Zval variable container to optimize memory usage. The second extra byte is "RefCount", which represents the number of variables (also known as symbols) that point to the Zval variable container. All symbols exist in a symbol table, where each symbol has scope (scope), and those main scripts (for example, scripts that are requested by the browser) and each function or method also have scopes.


Above to look at the bird Brother's 11 blog article, the PHP variable and memory management has a preliminary understanding.

PHP Note variables and memory management

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.