In-depth analysis of PHP memory management who moved my memory _php tutorial

Source: Internet
Author: User
First let's look at a problem: the output of the following code,
Copy CodeThe code is 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)

Notice 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 these 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, only you display the call memory allocation related APIs will have memory allocation.
In other words, there are a lot of memory allocation procedures that we don't see in PHP.
For example:
$a = "laruence";
The implicit memory allocation points are:
1.1. Allocating memory to the variable name and storing it in the symbol table
2.2. Allocating memory for variable values
So, you can't just look at appearances.
Second, don't doubt that PHP's unset does release memory, but this release is not a C programming sense release, not a return to the OS.
For PHP, it provides itself with a set of memory management APIs similar to the C language for memory allocation:
Copy CodeThe code is as follows:
Emalloc (size_t size);
Efree (void *ptr);
Ecalloc (size_t nmemb, size_t size);
Erealloc (void *ptr, size_t size);
Estrdup (const char *s);
Estrndup (const char *s, unsigned int length);

These APIs correspond to the API meanings of C, which are managed by these APIs within PHP.
When we call Emalloc to request memory, PHP is not simply to the OS to memory, but will be like the OS to a chunk of memory, and then assign one piece to the applicant, so when there is logic to request memory, it is no longer necessary to the OS to request memory, avoid frequent system calls.
For example, the following examples:
Copy CodeThe code is as follows:
Var_dump (Memory_get_usage (TRUE)); Note that the real_size is getting
$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 does not request new memory from the system.
Similarly, when we call Efree to release memory, PHP does not return the memory to the OS, but it will put this memory into the list of free memory that it maintains. For small chunks of memory, it is more likely to put it in the memory cache list (PostScript, some versions of PHP, such as my verified PHP5.2.4, 5.2.6, 5.2.8, in the call Get_memory_usage (), does not subtract the size of the available memory block in the memory cache list, resulting in the appearance that the memory will not change after unset.

Now let me answer these 32 bytes where to go, as I just said, a lot of memory allocation process is not explicit, read the following code you understand:
Copy CodeThe code is as follows:
Var_dump ("I am laruence, from http://www.laruence.com");
Var_dump (Memory_get_usage ());
$a = "laruence";
Var_dump (Memory_get_usage ());
unset ($a);
Var_dump (Memory_get_usage ());
Output:
String ("I am laruence, from http://www.laruence.com")
Int (90808)//before assignment
Int (90976)
Int (90808)//Yes, the memory is normally released.
90808-90808 = 0, normal, that is to say, these 32 bytes are consumed by the output function (strictly speaking, is the output of the header occupied)

an array that only increases in weight
Hashtable is the core structure of PHP, and the array is represented by her, and the symbol table is an associative array for the following code:
Copy CodeThe code is as follows:
Var_dump ("I am laruence, from http://www.laruence.com");
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:
Copy CodeThe code is as follows:
String ("I am laruence, from http://www.laruence.com")
Int (93560)
Int (118848)
Int (104448)

Wow, why is it so much less memory?
This is because for Hashtable, when defining it, it is not possible to allocate enough memory block at a time to save the unknown number of elements, so PHP will be initialized at the time, just allocate a small portion of memory to Hashtable, when not enough time 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 to do a single expansion, and when we unset out these 100 variables in turn, the variable occupied 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 preliminary understanding of the memory management of PHP?

http://www.bkjia.com/PHPjc/327674.html www.bkjia.com true http://www.bkjia.com/PHPjc/327674.html techarticle first let's look at a problem: the output of the following code, copy the code as follows: Var_dump (Memory_get_usage ()); $a = "laruence"; Var_dump (Memory_get_usage ()); Unset ( $a); Var_dump (M ...

  • Related Article

    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.