Home >Backend Development >PHP Tutorial >Will PHP's unset release memory?_PHP Tutorial
First let us look at an example
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
Notice that 90472-90440=32, so there are various conclusions. Some people say that PHP’s unset is not It does not really release memory. Some say that PHP's unset only truly frees memory when it releases large variables (a large number of strings and large arrays). Some 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:
These 32 bytes Where did it go?
First of all, we have to break a thought: PHP is not like the C language. Memory allocation will occur only if you explicitly call the memory allocation related API.
In other words, in PHP, There are many memory allocation processes that we cannot see.
For example:
$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 the memory (of course, it must be combined with references and counting. For this part, please refer to my previous article on in-depth understanding of PHP principles of variable separation/reference), but this release is not a release in the sense of C programming, and it is not handed back 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 meanings of C APIs. Memory is managed internally in PHP 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 block of memory, and then allocates a piece of it to the applicant, so that 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:
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 did not apply for new memory from the system.
Same as
In this way, 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,
More likely, put it in the memory cache list (postscript, some versions of PHP, such as PHP5.2.4, 5.2.6, 5.2.8, which I verified,
When calling get_memory_usage(), the available memory block size in the memory cache list will not be subtracted, causing it to appear that the memory remains unchanged after unset).
Now let me answer this 32-byte run Where did it go? As I just said, many memory allocation processes are not explicit. You will understand after reading the following code:
var_dump("I am www .7di.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.7di.net"
int(90808) //Before assignment
int(90976 )
int(90808) //Yes, the memory is released normally
90808-90808 = 0, which is normal, that is to say, these 32 bytes are occupied by the output function (strictly speaking , is occupied by the output Header)
An array that only increases but never decreases
Hashtable is the core structure of PHP (to understand Hashtable, you can refer to my previous article for an in-depth understanding of PHP Array (traversal order)), the array is also represented by her, and the symbol table is also an associative array, for the following code:
var_dump("I am www.7di.net");
var_dump(memory_get_usage());
$array = array_fill(1, 100, "laruence");
foreach ($array as $key => $value) {
${$value . $ key} = NULL;
}
var_dump(memory_get_usage());
foreach ($array as $key=> $value) {
unset(${$value . $key}) ;
}
var_dump(memory_get_usage());
We defined 100 variables, and then clicked Unset to see the output:
string(43) "I am www.7di.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 one time , to save an unknown number of elements, so PHP will only allocate a small part of the memory block to HashTable during initialization, and then RESIZE to expand the capacity when it is not enough,
And Hashtable,
It can only expand the capacity, not reduce it. For the above example, when we stored 100 variables, the symbol table was not enough, so we did an expansion.
When we unset these 100 variables in sequence, the memory occupied by the variables is released (118848 – 104448), but the symbol table is not reduced.
So the small amount of memory is occupied by the symbol table itself...
Now, do you have a preliminary understanding of PHP's memory management?