Will PHP unset release the memory? _ PHP Tutorial

Source: Internet
Author: User
Will PHP unset release the memory ?. First, let's look at an example of var_dump (memory_get_usage (); $ alaruence; var_dump (memory_get_usage (); unset ($ a); var_dump (memory_get_usage ()); output (on my PC, let's look at an example first.

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:

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:

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?

Using var_dump (memory_get_usage (); $ a = "laruence"; var_dump (memory_get_usage (); unset ($ a); var_dump (memory_get_usage ()); output (on my PC...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.