Hudzilla.org - the homepage of Paul Hudson
Contents > Performance > Optimising your code Wish List | Report Bug | About Me ]

18.1.10     Be wary of garbage collection, part 1

This is NOT the latest copy of this book; click here for the latest version.

PHP performs garbage collection at three primary junctures:

  1. When you tell it to

  2. When you leave a function

  3. When the script ends

Situation 1 occurs when you use unset(), mysql_free_result(), or other resource-destroying functions that explicitly clear up after your variables. Situation 2 clears up resources implicitly - any variable that leaves scope, i.e. is no longer applicable, gets cleared up for you. Finally, situation 3 frees up all script-related resources implicitly.

It is important to understand that situations 2 and 3 both use the same code path as situation 1 - that is, if you free up a resource using unset() or another resource-destroying function, it will take as much time to do as if PHP had to destroy the resource at the end of the script. This might sound like a rather mundane and, quite frankly, obvious thing to say, however it has profound implications because it means there is little reason to rely on PHP for garbage collection unless you are lazy!

Now, of course that is not quite true - unless you are really strapped for resources, there is little point calling unset() on each of your variables when you are done with them, and similarly there is little point in explicitly cleaning up after a resource just before the script is about to end, as it is going to happen anyway without the need to clog up your script. However, if there is any sizeable delay between you finishing using a big resource, and the juncture at which automatic garbage collection will take place (usually script end, but might also be function end), then you should clean up after yourself and claim the resource back.





<< 18.1.9 Don\'t think that using references will lower your RAM usage   18.1.11 Be wary of garbage collection, part 2 >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow