A deep understanding of PHP memory management who moved my memory ____php

Source: Internet
Author: User
reproduced in the blog of laruence great god Original address: http://www.laruence.com/2011/03/04/1894.html
Mar 11 Deep understanding of PHP memory management who moved my memoryAuthor: laruence () This article address: http://www.laruence.com/2011/03/04/1894.html reprint please indicate the source

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

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 are the 32 bytes ?

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:

$a = "laruence";

The implicit memory allocation points are:

1. Allocate memory for variable name, and deposit symbol table 2. Allocating memory for variable values

So, you can't just look at appearances.
Second, do not doubt that the PHP unset does release memory (of course, with reference and counting, this part of the content please see my previous article in-depth understanding of the PHP principle of the variable separation/reference), but this release is not a C programming sense of release, not handed back to the OS.
For PHP itself, it provides a set of memory management APIs similar to the C language for memory allocation:

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 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:

<?php Var_dump (Memory_get_usage (TRUE)); Attention to obtain 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, see comments.

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:

<?php 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 ());

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.