In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to manage memory in PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
First of all, let's look at a problem: the output of the following code
The copy code is as follows:
Var_dump (memory_get_usage ())
$a = "laruence"
Var_dump (memory_get_usage ())
Unset ($a)
Var_dump (memory_get_usage ())
Output (on my PC, it may vary depending on the system, PHP version, and loaded extensions):
Int (90440)
Int (90640)
Int (90472)
Note that 90472-90440mm 32, so there are a variety of conclusions, some people say that PHP unset does not really release memory, some say that PHP unset only releases large variables (a large number of strings, large arrays) will really free memory, and some people say that it is meaningless to discuss memory at the PHP level.
So, will unset release memory after all?
Where are the 32 bytes?
To answer this question, I will start from two aspects:
Where are these 32 bytes?
First of all, we have to break a thinking: PHP is not like the C language, only you show the call memory allocation related to API will have memory allocation.
In other words, in PHP, there are a lot of memory allocation processes that we can't see.
For example, for:
$a = "laruence"
Implicit memory allocation points are:
1.1. Allocate memory for variable names and store them in the symbol table
2.2. Allocate memory for variable values
Therefore, we can't just look at appearances.
Second, don't doubt that PHP's unset does free memory, but this release is not a C programming release and is not returned to OS.
For PHP, it provides a set of memory management API similar to the C language for memory allocation:
The copy 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 API correspond to the API meaning of C, and memory is managed within the PHP through these API.
When we call emalloc to request memory, PHP does not simply ask OS for memory, but asks for a large chunk of memory like OS, and then allocates a piece of memory to the applicant, so that when there is logic to apply for memory, it is no longer necessary to apply for memory from OS, avoiding frequent system calls.
For example, the following example:
The copy code is as follows:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.