Home > Backend Development > PHP Problem > How php manages memory

How php manages memory

(*-*)浩
Release: 2023-02-24 14:26:01
Original
1849 people have browsed it

Memory management generally includes the following:

How php manages memory

Whether there is enough memory for our program Usage;

How to obtain part of the memory from enough available memory;

For the used memory, whether it can be destroyed and reallocated to other programs for use. (Recommended learning: PHP programming from entry to proficiency)

Correspondingly, PHP’s memory management also includes such content, but these memories are in the form of macros in the ZEND kernel The form is provided as an interface for external use.

The latter two operations correspond to the emalloc macro and the efree macro respectively, and the first operation can be detected based on the result returned by the emalloc macro.

PHP’s memory management can be viewed as hierarchical. It is divided into three layers: storage layer (storage), heap layer (heap) and interface layer (emalloc/efree).

The storage layer actually applies for memory from the system through functions such as malloc() and mmap(), and releases the requested memory through the free() function. The storage layer usually applies for relatively large memory blocks. The large memory applied here does not refer to the memory required by the storage layer structure. It is just that when the heap layer calls the allocation method of the storage layer, it applies for it in large chunks. The role of the memory and storage layer is to make the memory allocation method transparent to the heap layer.

As shown in the figure, PHP memory manager. PHP has 4 memory allocation schemes in the storage layer: malloc, win32, mmap_anon, mmap_zero. By default, malloc is used to allocate memory. If the ZEND_WIN32 macro is set, it is the Windows version and HeapAlloc is called to allocate memory. The remaining two memory schemes are anonymous memory. Mapping, and PHP's memory scheme can be modified by setting environment variables.

How php manages memory

PHP内存管理机制
var_dump(memory_get_usage());   //获取内存
$a = "laruence";                //定义一个变量
var_dump(memory_get_usage());   //定义变量之后获取内存
unset($a);                      //删除该变量
var_dump(memory_get_usage());   //删除变量后获取内存

从上面可以看出php的内存管理机制是:预先给出一块空间,用来存储变量,当空间不够时,再申请一块新的空间。

1.存储变量名,存在符号表。

2.变量值存储在内存空间。

3.在删除变量的时候,会将变量值存储的空间释放,而变量名所在的符号表不会减小。

var_dump(memory_get_usage());  //获取内存
//定义100个变量
for($i=0;$i<100;$i++)
{
    $a = "test".$i;
    $$a = "hello";
}
//获取定义100个变量之后的内存
var_dump(memory_get_usage());
//定义100个变量并删除
for($i=0;$i<100;$i++)
{
    $a = "test".$i;
    unset($$a);
}
//获取删除之后的内存
var_dump(memory_get_usage());


从上面可以看出,虽然删除后内存变小了,但还是比没定义变量之前时大,这是因为虽然删除了变量的值,但变量名没有被删除。

php垃圾回收机制

PHP变量存储是存储在一个zval容器里面的

1.类型 2.值 3.is_ref 代表是否有地址引用 4.refcount 指向该值的变量数量

1.变量赋值的时候:is_ref为false  refcount为1

$a = 1;
xdebug_debug_zval(&#39;a&#39;);
echo PHP_EOL;

2.将变量a的值赋给变量b,变量b不会立刻去在内存中存储值,而是先指向变量a的值,一直到变量a有任何操作的时候

$b = $a;
xdebug_debug_zval(&#39;a&#39;);
echo PHP_EOL;

3.因为程序又操作了变量a,所以变量b会自己申请一块内存将值放进去。所以变量a的zavl容器中refcount会减1变为1,变量c指向a,所以refcount会加1变为2

$c = &$a;
xdebug_debug_zval(&#39;a&#39;);
echo PHP_EOL;
xdebug_debug_zval(&#39;b&#39;);
echo PHP_EOL;
Copy after login

Garbage collection:

1. In version 5.2 or earlier, PHP will determine whether it is garbage based on the refcount value

If the refcount value is 0, PHP will release it as garbage

This recycling mechanism is defective, and the variables with circular references cannot be recycled

2. After 5.3 This version improves the garbage collection mechanism

If it is found that the refcount in a zval container is increasing, it means it is not garbage

If it is found that the refcount in a zval container is decreasing, if it is reduced to 0, it is directly regarded as Garbage collection

If it is found that the refcount in a zval container is decreasing and has not decreased to 0, PHP will put the value into the buffer as a possible suspect object of garbage.

When the buffer reaches the critical value, PHP will automatically call a method to traverse each value, and clean it if it is found to be garbage

The above is the detailed content of How php manages memory. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template