Home >Backend Development >PHP Tutorial >Analyze whether unset in PHP will release memory_PHP tutorial

Analyze whether unset in PHP will release memory_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:00:51940browse

First let us look at an example

Copy the code The code is as follows:

var_dump(memory_get_usage ());
$a = "laruence";
var_dump(memory_get_usage());
unset($a);
var_dump(memory_get_usage());

Output (on my personal computer, it may vary depending on the system, PHP version, and loaded extensions):
int(90440)
int(90640)
int(90472

Noticed 90472-90440=32, so there are various conclusions. Some people say that PHP's unset does not really release memory, and some say that PHP's unset only releases large memory. Only when variables (a large number of strings, large arrays) are used, the memory will be truly free. Some people even say that it is meaningless to discuss memory at the PHP level.

So, will unset release memory? Where do these 32 bytes go?
To answer this question, I will start from two aspects :
Where did these 32 bytes go?

First of all we have to break a thought: PHP is not like the C language, only when you explicitly call the memory allocation related API. Memory allocation.
That is to say, in PHP, there are many memory allocation processes that we cannot see.
For example:

Copy code The code is as follows:

$a = "laruence";

The implicit memory allocation point is:
1. Allocate memory for the variable name and store it in the symbol table
2. Allocate memory for the variable value

So, you can’t just look at the appearance.
Second, Don’t doubt that PHP’s unset will indeed release memory (of course, it must also be combined with references and counting. Please refer to my previous article for an in-depth understanding of PHP principles of variable separation/reference for this part), but this Release is not a release in the sense of C programming, it is not a return to the OS.
For PHP, it provides a set of memory management APIs similar to the C language for memory allocation. These APIs correspond to the meaning of C APIs. Within PHP, memory is managed through these APIs.

When we call emalloc to apply for memory, PHP does not simply ask the OS for memory, but asks the OS for a large piece of memory, and then allocates a piece of it to the applicant, so that when there is logic When applying for memory, you no longer need to apply for memory from the OS, avoiding frequent system calls.
For example, the following example:

Copy code The code is as follows:
var_dump(memory_get_usage(TRUE)); //Note that what is obtained is real_size
$a = "laruence";
var_dump(memory_get_usage(TRUE));
unset($a);
var_dump(memory_get_usage(TRUE));

Output:
int( 262144)
int(262144)
int(262144

That is, when we define the variable $a, PHP does not apply for new memory from the system.

Similarly, when we call efree to release memory, PHP will not return the memory to the OS, but will add this memory to the free memory list it maintains. For small pieces of memory, it is more The possibility is to put it in the memory cache list (postscript, some versions of PHP, such as PHP5.2.4, 5.2.6, 5.2.8 that I have verified, will not decrease when calling get_memory_usage() Remove the available memory block size in the memory cache list, causing it to appear that the memory remains unchanged after unset).

Now let me answer where these 32 bytes go. As I just said, many memory allocation processes are not explicit. You will understand after reading the following code:

Copy code The code is as follows:

var_dump("I am www.jb51.net ");
var_dump(memory_get_usage());
$a = "laruence";
var_dump(memory_get_usage());
unset($a);
var_dump(memory_get_usage() );

Output:
string(43) "I am www.jb51.net"
int(90808) //Before assignment
int(90976)
int( 90808) //Yes, the memory is released normally

90808-90808 = 0, normal, that is to say, these 32 bytes are occupied by the output function (strictly speaking, they are occupied by the output Header)

Only Increasing array
Hashtable is the core structure of PHP (to understand Hashtable, you can refer to my previous article for an in-depth understanding of PHP arrays (traversal order)). Arrays are also represented by it, and symbol tables are also used. An associative array, for the following code:

Copy code The code is as follows:

var_dump("I am www.jb51. net");
var_dump(memory_get_usage());
$array = array_fill(1, 100, "laruence");
foreach ($array as $key => $value) {
       ${$value . $key} = NULL;
                                                                                                                      $value . $key});
}
var_dump(memory_get_usage());


We defined 100 variables, and then pressed Unset to see the output :
string(43) "I am www.jb51.net"
int(93560)
int(118848)
int(104448


Wow, why is there so much less memory?

This is because for Hashtable, when defining it, it is impossible to allocate enough memory blocks at once to store an unknown number. elements, so PHP will only allocate a small portion of the memory block to the HashTable during initialization, and then RESIZE to expand the capacity when it is not enough,
The Hashtable can only be expanded, not reduced. For the above example, when we stored 100 variables, the symbol table was not enough, so we did an expansion, and when we unset the 100 variables one by one Afterwards, the memory occupied by the variables is released (118848 – 104448), but the symbol table does not shrink, so the small memory is occupied by the symbol table itself...

Now, do you have a preliminary understanding of PHP’s memory management?

http://www.bkjia.com/PHPjc/328036.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328036.htmlTechArticleFirst let us look at an example to copy the code as follows: var_dump(memory_get_usage()); $a = "laruence" ; var_dump(memory_get_usage()); unset($a); var_dump(memory_get_usage()); Output...
Statement:
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