Entry
Does PHP automatically free MySQL results when the script ends?
Why does my continuously running MySQL script eat up memory?
How can I free the memory allocated for results by MySQL?
Jul 3rd, 1999 20:01
Nathan Wallace,
All memory used by PHP scripts is released when the request
is complete (except for persistent connections, of course).
But if you need to free it up, and then do some more PHP before the
script terminates you should use mysql_free_result, especially if you
are doing queries inside a loop or if your script runs for a long time
(eg: chat program).
Some people believe it's good programming practice to call
mysql_free_result() every time you are finished with the result set.
http://www.php.net/manual/function.mysql-free-result.php3
In PHP3 the results will be freed automatically at the end of the script
execution, but if you have a loop of several hundred iterations and you
don't free the result handles inside the loop, your script will have
hundreds of result sets allocated before it ends.
In PHP 4.0, result handles are freed automatically as soon as they're
no longer referenced, much like any other memory in PHP.
So, you could do:
$query1 = mysql_query(...);
$query2 = mysql_query(...);
and expect both query handles to be valid.
The result will be freed as soon as it's no longer referenced, and thus,
cannot be accessed by the user anyway. For example:
for (...) {
$query = mysql_query(...); // previous result will be freed here
// it's overwritten with the new result
}