PHPer please manually release your resources

Source: Internet
Author: User
Tags php book
I never thought this problem was a problem until yesterday. last night, I submitted an RFC about introducing finally to PHP. the starting point for implementing this function is very simple, because I have seen the needs of many people and Stas said, I can only see the discussion all the time ,... "> <LINKhref =" http://www.php100.com//statics/

I never thought this problem was a problem until yesterday.

Last night, I submitted an RFC about introducing finally to PHP. the starting point for implementing this function is very simple, because I have seen the needs of many people and Stas said, I only saw the discussion, but I didn't see any implementation. so I implemented it.

After being sent to the contact group, Nikita Popov (nikic), an developer from the development group, expressed strong opposition to this RFC. of course, he made a lot of arguments at the beginning. at the end of our online discussion, he expressed his opinion:

"PHP releases all resources after the request ends, so we do not need to call fclose or mysql_close to release the resource. PHP will do it for us"

In addition, he said that he would never call fclose and thought that fclose existed only to inherit the C function family.

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

Before PHP5.2, PHP used Reference count for resource management. when the Reference count of a zval is 0, it will be released. although Cycle reference exists, this design is no problem for the development of Web scripts, because the characteristics of Web scripts and the goal it pursues are short execution time, does not run for a long time. resource leakage caused by cyclic references will be released at the end of the request. that is to say, releasing resources at the end of a request is a remedy (backup ).

However, as PHP is used by more and more people, many people use PHP in some background scripts. these scripts are characterized by long-term running, as a result, the reference count cannot release unused resources in time, and the script will eventually run out of memory.

So after PHP5.3, we introduced GC, which is to say that we introduced GC to solve the problems that users cannot solve.

This is history. in my brief introduction, let's look back at the question at the beginning. Is it because PHP will release all resources after the request ends, so we don't need to release them manually?

Let's look at an example:

Mysql Max connections (mysql. max_connections)

$ Db = mysql_connect ();
$ Resut = mysql_query ();
// Process result...
Usleep (500 );
 
// Mysql_close ($ db); let's say, you didn't call to this
 
// Other logic, assuming it costs 5S
Sleep (5 );
 
Exit (0); // finish

 

In the above example, we will keep a connection with Mysql for 5 seconds. such a script does not matter for general applications, but for a script with a large request volume, this will cause a fatal problem:

For example, if a busy application processes 1000 requests per second from a user, how many requests are sent in five seconds? 5*1000 = 5000, while Mysql has the maximum number of connections (mysql. max_connections). This number generally does not exceed 2000. the default value is lower: (mysql. max_connections ),

Then, this code will cause your application to be unable to provide services normally. if we close this connection after processing Mysql, this issue will not be triggered.

In practice, we have encountered a more practical problem. let's look at the following example:

$ Mmc = new Memcached ();
$ Mysql = mysql_connect ();
// Process
Mysql_close ($ mysql );
$ Mmc-> close ();

 

This is a real lesson. as shown in the code above, suddenly a problem occurs in Mysql one day, resulting in the time consumption for connecting to Mysql increasing. then, a script occupies too long for Memcached connections, in the end, Memcache rejects the service because of too many connections ..

Therefore, we must first initialize the resources with the highest connection cost.

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

This is very simple. if you keep opening the handle without releasing it, you may trigger the maximum handle limit of the system. for the process, you can also set the number of handles (ulimit-n) for a process ).

System call is expensive)

PHP correctly releases all resources and memory after the request ends, because when we use new memory in the script, PHP will apply for a large block of memory (ZEND_MM_SEG_SIZE) from the OS, and then allocate the appropriate small block of memory to you.

When you do not use this small memory, PHP will not return it to the OS, but will keep it for subsequent processing.

We know that malloc (3) will lead to system calling (brk (2) (of course it may also be mmap. we will not consider this details here, thanks to Chinese ), system calls are expensive.

Therefore, if you have used up resources and do not release them in time, if you request the memory in the subsequent logic, PHP will find that a large part of memory has been split, it had to initiate a malloc call to the OS again to get a new large memory. and it also needs to mark this large memory ..

If you release resources in time, the memory block you returned will be reused the next time the script applies for memory, maybe your entire script only needs to apply for memory once with the OS.

Memory peak (Memory peak usage)

This has something to do with the above. after you use the resources, release them and use these resources later. the memory usage of PHP will be:

Resource + 1-> resource-1-> resource + 1-> resource-1 (peak value is 1)

If you wait until the PHP request ends and then release it:

Resource + 1-> resource + 1 .... -> Resource-1-> resource-1 (peak value: 2)

That is to say, a good script may be worse than a blind script, saving a lot of peak memory ..

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

Conclusion)

The conclusion is obvious. I also mentioned it at the beginning. I never think this is a problem.

If you buy a PHP book, it tells you: "You don't have to release resources in PHP because PHP will release resources for you." I suggest you, burned 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.