Parsing PHP unset will not release memory _php tips

Source: Internet
Author: User

First let's look at an example

Copy Code code as follows:

Var_dump (Memory_get_usage ());
$a = "laruence";
Var_dump (Memory_get_usage ());
unset ($a);
Var_dump (Memory_get_usage ());

The output (on my PC, may vary depending on the system, PHP version, load extension):
Int (90440)
Int (90640)
Int (90472

note that 90472-90440=32, so there are a variety of conclusions, some people say that PHP unset does not really release memory, some say, PHP unset only in the release of large variables (a large number of strings, large arrays) when the real free memory, more people say , there is no point in discussing memory at the PHP level.

So, in the end unset will not release memory? Where did the 32 bytes go?
To answer this question, I'll start with two things:
Where did these 32 bytes go?

First we have to break a thinking: PHP is not like C language, only you show the call memory allocation related APIs will have memory allocation.
In other words, in PHP, there are many memory allocation processes that we don't see.
For example:

Copy Code code as follows:

$a = "laruence";

The implicit memory allocation points are:
1. Allocate memory for variable name, save in symbol table
2. Assign the value of the variable

So, you can't just look at appearances.
Second,Don't doubt that PHP's unset does release memory (and, of course, combine references and counts, this part of the article see my previous articles to understand the PHP principle of the variable separation/reference), but this release is not a C programming sense of release, not to return to the OS.
For PHP itself, it offers a set of memory management APIs similar to the C language for memory allocations, which correspond to the API meaning of C, which is used internally by these APIs to manage memory.

When we call Emalloc to request memory, PHP is not simply to the OS to the memory, but will be like the OS to a large chunk of memory, and then put a piece of it to the requester, so that when there is logic to apply for memory, it will no longer need to apply memory to the OS, avoid frequent system calls.
Examples include the following:

Copy Code code as follows:
<?php
Var_dump (Memory_get_usage (TRUE)); Note that the acquisition 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 variable $a, PHP does not request 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 put this memory into its own maintenance of the free memory list. And for small chunks of memory, it's more likely that you put it in the memory cache list (PostScript, some versions of PHP, such as PHP5.2.4, 5.2.6, and 5.2.8 that I've validated, when I call Get_memory_usage (), The free memory block size in the memory cache list is not subtracted, resulting in the appearance of unset memory.

Now let me answer these 32 bytes where to go, just to say to me, a lot of memory allocation process is not explicit, look at the following code you understand:

Copy Code code as follows:

<?php
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 ("I am Www.jb51.net")
Int (90808)//before assignment
Int (90976)
Int (90808)/Yes, memory normal release

90808-90808 = 0, normal, which means that the 32 bytes are occupied by the output function (strictly speaking, the header is occupied by the output)

An array that is only added and not reduced
Hashtable is the core structure of PHP (for Hashtable, see my previous article for an in-depth understanding of the PHP array (traversal order), the array is represented by her, and the symbol table is an associative array, for the following code:

Copy Code code as follows:

Var_dump ("I am Www.jb51.net");
Var_dump (Memory_get_usage ());
$array = Array_fill (1, "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 define 100 variables and then press a unset them to see the output:
String ("I am Www.jb51.net")
Int (93560)
Int (118848)
Int (104448

Wow, why is there so much memory missing?
This is because for Hashtable, when you define it, it is not possible to allocate enough chunks of memory at once to save an unknown number of elements, so PHP will be initialized, just allocate a small amount of memory block to Hashtable, when not enough to resize expansion,

And Hashtable, can only expand, will not be reduced, for the above example, when we deposit 100 variables, the symbol table is not enough, do a expansion, and when we sequentially unset the 100 variables, the variable occupies the memory is released (118848–104448), but The symbol table does not shrink, so these little memory is taken up by the symbol table itself ...

Now, do you have a rudimentary understanding of PHP's memory management?

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.