Phper, please release your resources manually.

Source: Internet
Author: User
Tags count memcached mysql php book reference resource rfc

I never thought the question was a problem until yesterday.

Last night, I submitted an RfC, about the introduction of finally to PHP, the starting point for this function is very simple, because I see a lot of people's needs, in addition to Stas said, has been only to see the discussion, did not see someone to achieve. So I came to the realization.

After sending to the Mail group, a group of students Nikita Popov (Nikic), expressed strong opposition to this RfC, of course, the original argument he said a lot, and finally we discussed online, he expressed a point of view:

"PHP releases all the resources after the request, so we don't need to call fclose, or mysql_close to release the resources, PHP will do for us"

And he said he would never call fclose, thinking that fclose exists just to inherit the C-function family.

I was surprised that I didn't know how many people had the same idea, so I decided to write this article.

Before PHP5.2, PHP used reference counting (Reference count) to manage resources, and when a Zval reference count was 0, it was released. Although there is a circular reference (Cycle reference), such a design is not a problem for developing web scripts, because the characteristics of Web scripts and the goal it pursues are short execution times and not long-running. A resource leak caused by a circular reference is released at the end of the request. In other words, releasing resources at the end of a request is a part of the remedial action (backup).

However, as PHP is used by more and more people, there are a lot of people in some background scripts using PHP, these scripts are characterized by long run, if there is a circular reference, causing the reference count can not release the unused resources in time, then the script will eventually run out of memory.

So after PHP5.3, we introduced GC, which means we introduced GC to solve problems that users can't solve.

This is history, I briefly introduce, now let us look back at the beginning of the question, is not because PHP will be at the end of the request to release all the resources, so we can not manually release it?

Look at an example:

maximum number of MySQL connections (mysql.max_connections)

<?php
$db = mysql_connect ();
$resut = mysql_query ();
Process result ...
Usleep (500);

Mysql_close ($DB); Let ' s say, your didn ' t call to this

Other logic, assuming it costs 5s
Sleep (5);

Exit (0); Finish

In the example above, we will keep a connection with MySQL for 5 seconds, such a script for the general application does not matter, but for a large request script, can cause a fatal problem:

For example, a busy application, 1000 requests per second to be processed from the user, then how many 5 seconds request? 5 * 1000 = 5000, and MySQL has the maximum number of connection limits (mysql.max_connections), this number is generally not more than 2000, the default will be lower: (mysql.max_connections),

So, this code will cause your application and simply not be able to provide the service properly. And if we close the connection after the MySQL processing is complete, then this problem will not be triggered.

And we have encountered a more practical problem in practice, looking at the following example:

<?php
$MMC = new Memcached ();
$mysql = mysql_connect ();
Process
Mysql_close ($mysql);
$mmc->close ();

This is a real lesson, the code as shown above, suddenly one day our MySQL problem, resulting in the time to connect MySQL increased, and then led to a script on the memcached connection too long, and finally memcache because the number of connections too much, it refused service.

Therefore, we must first initialize the resources that have the highest cost of connection.

system Max Handle (/proc/sys/fs/file-max)

This is very simple, if you continue to open the handle without releasing it, then you have the possibility to trigger the system maximum handle limit, for the process, there is a process to open the handle number limit (ulimit-n).

system calls are expensive (systems call is expensive)

PHP will correctly release all the resources, memory, after the request is finished, because when we use the new memory in the script, PHP will request a large chunk of memory (zend_mm_seg_size size) to the OS, and then give you the appropriate piece of small memory you need.

When you do not use this small memory, PHP will not be returned to the OS, but retained for subsequent processing use.

We know that malloc (3) can lead to system calls (BRK (2)) (Mmap, of course, we don't consider this detail here, to Chinese), while system calls are expensive.

Therefore, if you use the resource is not released in time, then the subsequent logic if the request of memory, PHP found before the application of a large chunk of memory has been divided, it will have to again to the OS launched malloc call, get a new large memory. And it also needs to do some tagging on this big memory.

And if you use the resource and release it in time, the next time the memory block you return can be reused, your entire script needs only one memory for the OS.

Peak Memory (Memory peak usage)

This has a certain relationship with the above, when you use the resources to release, and then later use such resources. Then the memory footprint of PHP will be:

Resources +1-> Resources -1-> Resources +1-> Resources-1 (peak is 1)

And if you wait until the PHP request is over and then released:

Resources +1-> Resources + 1 ...-> resource -1-> resource –1 (peak is 2)

It's also said that a well-written script might be a lot more peak memory than a scripted script.

Consider an extreme situation, for a very busy server, for example, there are 10 PHP processes, each PHP process maximum 1G memory, and the server only 8G memory.

conclusion (conclusion)

The conclusion is obvious, as I said at the beginning, I never thought it was a problem.

Here's a word, if you buy a PHP book, it tells you: "Do not actively release resources in PHP, because PHP will help you release," I suggest you burn it.



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.