First, let's look at an example.
Var_dump (memory_get_usage ());
$ A = "laruence ";
Var_dump (memory_get_usage ());
Unset ($ );
Var_dump (memory_get_usage ());
Output (on my PC, the load extension may vary depending on the system, PHP version, and load ):
Int (90440)
Int (90640)
Int (90472
I noticed 90472-90440 = 32, so I came to various conclusions. Some people say that PHP's unset does not actually release the memory, and some say, PHP's unset only truly free memory when releasing large variables (a large number of strings, large arrays). Some people say that it is meaningless to discuss memory at the PHP level.
So Will unset release the memory? Where are these 32 bytes running?
To answer this question, I will start from two aspects:
Where are the 32 bytes?
First of all, we need to break the idea: PHP is not like the C language. You only need to call the memory allocation API to allocate memory.
That is to say, there are many memory allocation processes that we cannot see in PHP.
For example:
$ A = "laruence ";
Implicit Memory Allocation points are:
1. allocate memory for the variable name and store it to the symbol table.
2. Allocate the variable value
Therefore, you cannot just look at the appearance.
Second, do not doubt that PHP's unset will indeed release the memory (of course, it should be combined with reference and count, for more information about this part, see my previous article to deeply understand the PHP principle of variable separation/reference). However, this release is not a release in the C Programming sense, 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 c api meanings, these APIs are used internally in PHP to manage the memory.
When we call emalloc to apply for memory, PHP does not simply ask for memory from the OS, but wants a large block of memory like the OS, and then allocates one of them to the applicant, in this way, when there is another logic to apply for memory, you no longer need to apply for memory from the OS, avoiding frequent system calls.
For example:
<? Php
Var_dump (memory_get_usage (TRUE); // note that real_size is obtained.
$ A = "laruence ";
Var_dump (memory_get_usage (TRUE ));
Unset ($ );
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 the memory, PHP will not return the memory to the OS, but will include the memory in the idle memory list maintained by itself. for small pieces of memory, it is more likely to put it in the memory cache list (Note: some versions of PHP, such as PHP5.2.4, 5.2.6, 5.2.8 that I have verified, when get_memory_usage () is called, the available memory block size in the memory cache list is not subtracted, which makes it look like the memory will remain unchanged after unset ).
Now let me answer where the 32 bytes are running. As I said, many memory allocation processes are not explicit. After reading the following code, you will understand:
<? Php
Var_dump ("I am www.7di.net ");
Var_dump (memory_get_usage ());
$ A = "laruence ";
Var_dump (memory_get_usage ());
Unset ($ );
Var_dump (memory_get_usage ());
Output:
String (43) "I am www.7di.net"
Int (90808) // before value assignment
Int (90976)
Int (90808) // yes, the memory is released normally
90808-90808 = 0, normal, that is, the 32 bytes are occupied by the output function (strictly speaking, they are occupied by the output Header)
Only add and remove Arrays
Hashtable is the core structure of PHP (for more information about Hashtable, see my previous article to learn more about PHP arrays (traversal order). The arrays are also represented by her, the symbol table is also an associated 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 Unset them to see the output:
String (43) "I am www.7di.net"
Int (93560)
Int (118848)
Int (104448
Wow, why is so much memory missing?
This is because for Hashtable, when defining it, it is impossible to allocate enough memory blocks at a time to save the unknown number of elements, so PHP will during initialization, allocate a small amount of memory blocks to HashTable, and RESIZE it if it is not enough,
Hashtable can only be resized, but will not be reduced. For the above example, when we store 100 variables, the symbol table is not enough and a scale-up is performed, when we unset these 100 variables in turn, the memory occupied by the variables is released (118848-104448), but the symbol table is not reduced, so these few memories are occupied by the symbol table itself...
Now, do you have a preliminary understanding of PHP memory management?