Author: Laruence () address of this article: www.laruence.com201207252662.html. Please indicate the source. 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.
Author: Laruence () Address: http://www.laruence.com/2012/07/25/2662.html reprint please indicate the source I never think this problem is 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.
- Author: Laruence ()
- Address: http://www.laruence.com/2012/07/25/2662.html
- Reprinted please indicate the source
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)
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:
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.
Comments
- Writes: Sofa ~ Attention should be paid to the details ~
- 2012/07/25, Demon writes: Chinese: Nikita Popov you are not carved. English: Nikita Popov you're a genius!
- , Forever writes: the error message "Please release resources maunally" in the title is manually. Actually, this article hasn't seen it yet. Hey
- , Jason Gian writes
- , Miro writes: This is really not a problem. What is it about? When I was a child, I don't need to wipe my ass. I don't want to wipe my ass when I grow up ?!
- , Johnson writes: Sorry, young man.
- , Anonymous writes
- 2012/07/25, ixqbar writes: How can I package this RFC into php?
- 2012/07/25, lucky writes:, so how can I choose to reuse MYSQL links and destroy them instantly?
- 2012/07/25, lucky writes:, so how can I choose to reuse MYSQL links and destroy them instantly?
- , Greyson writes: no one can guarantee what will happen after your use. Why don't you manually release it. (Whispered: How to join the Contact Group, Sina employee. Thank you)
- , No PHP writes: some details are worth learning.
- , Chengdu + writes: [...] Author: Laruence address: http://www.laruence.com/2012/07/25/2662.htm [...]
- , Snow migratory birds writes: @ Greyson http://www.php.net/mailing-lists.php
- , Vincent writes: I think it's just the difference in the design concept of a language. Designers have different goals, either for machines or for writers. I have been talking about releasing resources since learning php, but is it okay not to release? Why?
- Writes: the PHP manual says in most cases there is no need to manually release the resource http://www.php.net/manual/en/language.types.resource.php#language.types.resource.self-destruct
- , The "hello, stranger ~ "Blog-Please manually release your resources (Please release resources manually) writes: [...] original address: http://www.laruence.com/2012/07/25/2662.html [...]
- , The "hello, stranger ~ "Blog-Please manually release your resources (Please release resources manually) writes: [...] original address: http://www.laruence.com/2012/07/25/2662.html [...]
- Sk writes: resources to be viewed. Haha, no need to release the handle for static variables
- , Stefan321 writes ~
- , Anonymous writes: Thank you very much for sharing details about this type of PHP!
- , Lxylxy888666 writes: A detailed problem that should be paid attention to when php scripts run for a long time, __release in destory is useless.
- , Toneyzeng writes: I strongly agree with the opinions of bloggers. For example, when using smarty, we usually execute parameter judgment, obtain data, and output smarty. As a result, many people write this statement: whether 3721 is used or not, first include a database. inc. php, then execute parameter judgment, get data, smarty output, and finally the bottleneck of the entire website is stuck in a large number of SQL connections. If you have the consciousness of releasing resources in advance, php will be more powerful, and finally is for try. There is no error in language feature support, it is useless for unconscious people to use the best tools, and for those who will use it, they will be babies.
- , Dumb writes: it can be a reminder for beginners like me. I hope I will pay attention to this issue in the future.
- , [Switch] Please manually release your resources (Please release resources manually)-xwuxin writes: [...] release resources manually) Author: snow (laruence) Original article link: http://www.laruence.com/2012/07/25/2662.html I never think this problem is a problem until yesterday. last night, [...]
- , Manually release your resources (Please release resources manually)-Dragon cat writes: [...] reprint from the snow: http://www.laruence.com/2012/07/25/2662.html [...]
- , Adeploy writes: I strongly agree with the question about the maximum number of connections of Mysql. Once I had a mass data processing problem, I had to process more than 10 million pieces of data and then I found that the mysql connection was not released... It took me a few days to find the bug.
- 2012/08/04, Zava writes: I don't know how it works? I personally think it is still different in concept. To put it bluntly, it is a different understanding of the positioning of PHP.
- , Please manually release your resources (Please release resources manually) forest/coffee Chengdu professional php website production | forest/coffee Chengdu professional php website production writes: [...] snow? PHP application Posted in: php/Tagged: manually, release, resources. Please manually release your resource Please [...]
- , Quminhui writes: "If you do not use this small memory, PHP will not return it to the OS, but it will be retained for subsequent processing. "Excuse me: During the runtime of PHP processing a request, does this mean that PHP only applies for system memory usage and does not release it?
- , 0xFP writes: PHP each request memory is a piece of memory (ZEND_MM_SEG_SIZE, php-5.4.3 is 256 Kib), when this large block of memory is idle will be returned to the system! A memory may store multiple variables. This memory is idle only when these variables are released!
- , 0xFP writes: php-r 'echo "original memory: \ t", memory_get_usage (true)/1024, "\ n"; $ x = str_repeat ("", 100*1024); echo "100*1024: \ t", memory_get_usage (true)/1024, "\ n"; $ x = str_repeat ("", 150*1024); echo "150*1024: \ t", memory_get_usage (true)/1024, "\ n"; $ x = str_repeat ("", 100*1024); echo "100*1024: \ t", memory_get_usage (true)/1024, "\ n"; $ x = str_repeat ("", 300*1024); echo "300 * 1024: \ t ", memory_get_usage (true)/1024," \ n "; $ x = str_repeat (" a ", 200*1024); echo" 200*1024: \ t ", memory_get_usage (true)/1024," \ n "; 'original memory: 256 // these memories include overhead other than variables 100*1024: 256150*1024: 512 // insufficient memory, 256 Kib100 * 1024: 512 at a time //????? The size of the same string, but the memory requested from the system is different from the previous one. It seems that the overhead other than the variable is in use. Why? 300*1024: 768 // one-time increase of 256 Kib200 * 1024: 512 // One-time decrease of 256 Kib
- , Anonymous writes: PHP was not good at solving this problem. For example, the mysql access method mentioned in this article is synchronous, and it is easy to see that the number of connections is too large. manually releasing resources only solves some problems. in this case, we usually make a proxy layer to process the connection from php to the backend service.
- 2012/08/09, wonderful things-reading xlinblog.sinaapp.com in a short literary story» Blog Archive» Please manually release your resources (Please release resources manually) writes: [...] address: http://www.laruence.com/2012/07/25/2662.html [...]
- 2012/08/14, PHP Tutorial: Please manually release your resources! | PHP fans writes: [...] original address: http://www.laruence.com/2012/07/25/2662.html [...]
- , Xbzbing writes: I seem to have a php book that can be burned...
- , No light writes in the wilderness: I like the last sentence ~~
- , Mysql basic usage | Lellansin's ice Sen writes: [...] after the $ res result set, you must release the resource in time using the mysql_free_result () method. For the reason, see "manually release your resource (Please release resources manually)" mysql_close () [...]
- 2012/09/14, wonderful things-reading xlinblog.sinaapp.com in a literary qutan novel again, do not use (include/require) _ once writes: [...] please manually release your resources (Please release resources manually) [...]
- Ling2/09/15, lingcarzy writes: Sorry, I learned the books that can be burned to start my php journey.
- , PHP, need to manually release resources? | Popular cloud writes: [...] Please manually release your resources (Please release resources manually) [...]
- , PHP Sucks writes: php makes business logic, that is, it cannot help the wall. Why should we let it do! How many websites can directly access the database in php?
- , Anonymous writes: what my colleague said makes sense. You are only talking about the case. Of course, it is better to manually release the package after the final compromise.
- , Carl writes: a typical C programmer, But I have released it manually. In PHP5.2, I encountered a second release error every time. I don't care about it anymore. Fortunately, I didn't write any large concurrent programs.
- , Please manually release your resources (Please release resources manually) | afternoon nap writes: [...] This article address: http://www.laruence.com/2012/07/25/2662.html [...]
- , Lonely student writes: learning ......
- 2014/01/13, Wang Wenlin writes: the reason for C ++ is not finally, but PHP is... Destructor and RAII are only legends?
- , Http://www.communitywalk.com/JBPlumbing-DiamondBar writes: There are many Lethbridge plumbing companies in this city which provide full service of plumbing. because of the perceived "snob appeal" I mentioned earlier, however, the fact that you have copper plumbing adds resale value to your home. installation and repair of bathroom and kitchen water systems.
- 2014/07/18, change writes: I love it whenever people get together and share ideas. Great website, continue the good work!
- , Wood dragon writes: Go to burn books ..
Related posts:
- Var_export and var_dump are different.
- HTTP_HOST in HTTP1.0 is empty
- Research on Asynchronous call methods in PHP
- More easily reproduce the PHP Core call stack
- You may not know about PHP-PHP event-driven design
Copyright©2010 All Rights Reserved. this Feed is for personal use only. reposted or commercial applications that are not specified are prohibited. if the application is illegal, all legal consequences shall be borne by you. if you have any questions, you can send an email to my at laruence.com. (Digital Fingerprint: 73540ba0a1738d7d07d4b6038d5615e2)