preface

The Garbage collector is a Garbage Collection.

Earlier versions, specifically before 5.3 (not including 5.3), did not have a dedicated garbage collector. If the refcount of zval is 0, the refcount of zval is 0, and the refcount of zval is 0.

At first glance, there is no problem, but there is a hidden risk of variable memory overflow: bugs.php.net/bug.php?id=… Unrecoverable memory causes memory leaks, so PHP5.3 has a GC dedicated to cleaning up garbage data and preventing memory leaks.

PHP reference counting basics

Refcount: The number of variables that are the same using the same value. Is_ref: type bool. When refcount is greater than 2, one of the variables is assigned with the address & and, well, it becomes 1.

I’m going to show you how to use PHP to visually see the changes in these counts. The first step is to install the Xdebug extension in PHP.

String variable

1. View the internal structure


      
    $name = "Public: PHP Open Source Community";
    xdebug_debug_zval('name');
Copy the code

Will get:

name:(refcount=1, is_ref=0),string 'Public account: PHP Open Source Community' (length=18)
Copy the code

2. Add a count


      
    $name = "Public: PHP Open Source Community";
    $temp_name = $name;
    xdebug_debug_zval('name');
Copy the code

Will get:

name:(refcount=2, is_ref=0),string 'Public account: PHP Open Source Community' (length=18)
Copy the code

See, refcount+1.

3. Reference assignment


      
    $name = "Public: PHP Open Source Community";
    $temp_name = &$name;
    xdebug_debug_zval('name');
Copy the code

Will get:

name:(refcount=2, is_ref=1),string 'Public account: PHP Open Source Community' (length=18)
Copy the code

Yes reference assignment causes Zval to flag the presence of a reference via is_ref.

An array of variables


      
    $name = ['a'= >'Public Account'.'b'= >PHP Open Source Community];
    xdebug_debug_zval('name');
Copy the code

Will get:

name:
(refcount=1, is_ref=0),
array (size=2)
  'a' => (refcount=1, is_ref=0),string 'Public Account' (length=9)
  'b' => (refcount=1, is_ref=0),string PHP Open Source Community (length=9)
Copy the code

Zval refount and IS_ref each maintain a set of zval refount and IS_ref.

Destruction of a variable


      
    $name = "Public: PHP Open Source Community";
    $temp_name = $name;
    xdebug_debug_zval('name');
    unset($temp_name);
    xdebug_debug_zval('name');
Copy the code

Will get:

name:(refcount=2, is_ref=0),string 'Public account: PHP Open Source Community' (length=18)
name:(refcount=1, is_ref=0),string 'Public account: PHP Open Source Community' (length=18)
Copy the code

Refcount is reduced by 1, indicating that unset does not necessarily free memory. When two variables are pointed to, the memory occupied by the variable is not freed, but the refcount is reduced by 1.

PHP memory management mechanism

Now that you know what zval is all about, let’s see how you can visualize the memory management mechanism in PHP.

External memory changes


      
    // Get memory method, true returns real memory, otherwise returns presentation memory
    var_dump(memory_get_usage());
    $name = "Public: PHP Open Source Community";
    var_dump(memory_get_usage());
    unset($name);
    var_dump(memory_get_usage());
Copy the code

Will get:

int 1593248
int 1593384
int 1593248
Copy the code

General process: define variables -> memory increase -> clear variables -> memory restore

Potential memory changes

When executed, memory allocation does two things:

1. Allocate memory for variable names to the symbol table

2. Allocate memory for variable values

$name = "Public: PHP Open Source Community";
Copy the code

Now look at the code:


      

    var_dump(memory_get_usage());
    for($i=0;$i<100;$i{+ +)$a = "test".$i;
        $$a = "hello";    
    }
    var_dump(memory_get_usage());
    for($i=0;$i<100;$i{+ +)$a = "test".$i;
         unset($$a);    
    }
    var_dump(memory_get_usage());
Copy the code

Will get:

int 1596864
int 1612080
int 1597680
Copy the code

It’s a blast. How is it different from what it looked like? Memory is not fully reclaimed.

For PHP’s core structure, Hashtable, it is not possible to allocate enough memory blocks at once because of the unknowability of the definition. When 100 variables are stored, the symbol table will be expanded once. When unset, only “allocate memory for variable values” will be freed. The “allocate memory for variable names” is in the symbol table. The symbol table is not shrinking, so the confiscated memory is occupied by the symbol table.

Potential memory requisition and release design

PHP and C language, also need to allocate memory, but these operations are encapsulated in the bottom layer, PHP users do not know it.

PHP does not simply request memory from the OS. Instead, it requests a large chunk of memory and allocates some of it to the applicant so that when logic requests memory again, it does not need to request memory from the OS, avoiding frequent calls. When the memory is insufficient, it will request again

When it frees memory, PHP does not return it to the OS, but rather places the memory track itself as a self-maintained list of free memory for reuse.

The above content hopes to help you, more free PHP factory PDF, PHP advanced architecture video materials, PHP wonderful good article can be wechat search concerns: PHP open source community

2021 Jinsanyin four big factory interview real questions collection, must see!

Four years of PHP technical articles collation collection – PHP framework

A collection of four years’ worth of PHP technical articles – Microservices Architecture

Distributed Architecture is a four-year collection of PHP technical articles

Four years of PHP technical essays – High Concurrency scenarios

Four years of elite PHP technical article collation collection – database