In-depth analysis of PHP Memory Management

Source: Internet
Author: User
This article provides a detailed analysis of the memory in php. For more information, see

This article provides a detailed analysis of the memory in php. For more information, see

First, let's look at the problem: the output of the following code,

The Code is as follows:


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.1. allocate memory for the variable name and store it to the symbol table
2.2. allocate memory for variable values
Therefore, you cannot just look at the appearance.
Second, do not doubt that PHP's unset will indeed release the memory, but this release is not a C-programming release, not a return to the OS.
For PHP, it provides a set of Memory Management APIs similar to the C language for memory allocation:

The 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 APIs of C, which 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:

The Code is as follows:


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:

The Code is as follows:


Var_dump ("I am Laruence, From ");
Var_dump (memory_get_usage ());
$ A = "laruence ";
Var_dump (memory_get_usage ());
Unset ($ );
Var_dump (memory_get_usage ());
Output:
String (43) "I am Laruence, From"
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, and arrays are represented by her. symbol tables are also an associated array. For the following code:

The Code is as follows:


Var_dump ("I am Laruence, From ");
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:

The Code is as follows:


String (43) "I am Laruence, From"
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?

, Record-free space, website space, U.S. servers

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.