Home >Backend Development >PHP Tutorial >Analyze whether unset in PHP will release memory_PHP tutorial
First let us look at an example
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:
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:
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:
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:
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