Will PHP's unset release memory?_PHP Tutorial
First let us look at an example
var_dump(memory_get_usage());
$a = "laruence";
var_dump(memory_get_usage());
unset( $a);
var_dump(memory_get_usage());
Output (on my personal computer, it may vary depending on the system, PHP version, and loaded extensions):
int(90440)
int(90640)
int(90472
Notice that 90472-90440=32, so there are various conclusions. Some people say that PHP’s unset is not It does not really release memory. Some say that PHP's unset only truly frees memory when it releases large variables (a large number of strings and large arrays). Some even say that it is meaningless to discuss memory at the PHP level.
So, will unset release memory? Where do these 32 bytes go?
To answer this question, I will start from two aspects:
These 32 bytes Where did it go?
First of all, we have to break a thought: PHP is not like the C language. Memory allocation will occur only if you explicitly call the memory allocation related API.
In other words, in PHP, There are many memory allocation processes that we cannot see.
For example:
$a = "laruence";
The implicit memory allocation point is:
1. Allocate memory for the variable name and store it in the symbol table
2. Allocate memory for the variable value
So, you can’t just look at the appearance.
Second, don’t doubt that PHP’s unset will indeed Release the memory (of course, it must be combined with references and counting. For this part, please refer to my previous article on in-depth understanding of PHP principles of variable separation/reference), but this release is not a release in the sense of C programming, and it is not handed back to the OS.
For PHP, it provides a set of memory management APIs similar to the C language for memory allocation. These APIs correspond to the meanings of C APIs. Memory is managed internally in PHP through these APIs.
When we call emalloc to apply for memory, PHP does not simply ask the OS for memory, but asks the OS for a large block of memory, and then allocates a piece of it to the applicant, so that there is logic When applying for memory, you no longer need to apply for memory from the OS, avoiding frequent system calls.
For example, the following example:
var_dump(memory_get_usage (TRUE)); //Note that what is obtained 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 the variable $a , PHP did not apply for new memory from the system.
Same as
In this way, when we call efree to release memory, PHP will not return the memory to the OS, but will add this memory to the free memory list it maintains. For small pieces of memory,
More likely, put it in the memory cache list (postscript, some versions of PHP, such as PHP5.2.4, 5.2.6, 5.2.8, which I verified,
When calling get_memory_usage(), the available memory block size in the memory cache list will not be subtracted, causing it to appear that the memory remains unchanged after unset).
Now let me answer this 32-byte run Where did it go? As I just said, many memory allocation processes are not explicit. You will understand after reading the following code:
var_dump("I am www .7di.net");
var_dump(memory_get_usage());
$a = "laruence";
var_dump(memory_get_usage());
unset($a);
var_dump (memory_get_usage());
Output:
string(43) "I am www.7di.net"
int(90808) //Before assignment
int(90976 )
int(90808) //Yes, the memory is released normally
90808-90808 = 0, which is normal, that is to say, these 32 bytes are occupied by the output function (strictly speaking , is occupied by the output Header)
An array that only increases but never decreases
Hashtable is the core structure of PHP (to understand Hashtable, you can refer to my previous article for an in-depth understanding of PHP Array (traversal order)), the array is also represented by her, and the symbol table is also an associative array, for the following code:
var_dump("I am www.7di.net");
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 clicked Unset to see the output:
string(43) "I am www.7di.net"
int(93560)
int (118848)
int(104448
Wow, why is there so much less memory?
This is because for Hashtable, when defining it, it is impossible to allocate enough memory blocks at one time , to save an unknown number of elements, so PHP will only allocate a small part of the memory block to HashTable during initialization, and then RESIZE to expand the capacity when it is not enough,
And Hashtable,
It can only expand the capacity, not reduce it. For the above example, when we stored 100 variables, the symbol table was not enough, so we did an expansion.
When we unset these 100 variables in sequence, the memory occupied by the variables is released (118848 – 104448), but the symbol table is not reduced.
So the small amount of memory is occupied by the symbol table itself...
Now, do you have a preliminary understanding of PHP's memory management?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
