Php5.2.0 memory management improvement

Source: Internet
Author: User
Php5.2.0 memory management improvement php5.2.0 memory management has been greatly improved. in some cases, the memory cannot be released.
Test the php script (mem. php). I use echo N> and sleep to control the pause of the script at a certain stage for detection.

The code is as follows:


Echo '1> ';
Sleep (5 );

$ O = array ();
For ($ I = 0; $ I <= 100000; $ I ++ ){
$ O [] = 'aaaaaaaaaaaaaaaaaaaaaaaaa ';
}
Echo '2> ';
Sleep (5 );

Unset ($ o );
Echo '3> ';
While (true ){
Echo '..';
Sleep (10 );
}
?>

Bash script that monitors memory usage (note: The "mem" in it is taken from the above php script name ):

While true; do clear; ps au | grep-v "\ (vi \ | grep \)" | grep "\ (mem \ | RSS \)"; sleep 2; done;
The following are $/usr/local/bin/php mem. php uses PHP 5.1.6 and 5.2.0 in three states (before the array is created, after the array is created, and after the array is destroyed) (I used the same configure parameter) test result of RSS (memory usage value, unit: KB.

Php5.1.6:
3164
18076
17572

PHP5.2.0:
4088
14400
4424
We can see that the memory is not released from the process after the unset array version 5.1.6, although it can be reused by the php process, but it cannot be used by other processes of the system. 5.2.0 actually releases the memory.

We may also note that at the beginning, the memory usage of 5.2.0 was several more kb than that of 5.1.6, which is due to the addition of some new things in 5.2.0, which is quite normal.

In addition, the memory allocation of php5.2.0 has also been greatly improved. The official statement is that the detection of memory_limit is performed by calling emalloc () each time () and then directly checks the memory data blocks (blocks) requested by the system ). You can study the code by yourself. Because of the changes made to the memory allocation implementation, the memory control can be more precisely controlled under memory_limit. that is to say, in the previous php code, if the memory usage exceeds memory_limit, there is no error, an error may be reported in php5.2.0. To balance this improvement, the default memory_limit of PHP5.2.0 was changed from 8 MB to 16 MB. search for the source code to see this change (find. -name \ * c-type f | xargs cat | grep memory_limit ).

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.