PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
function foo()
{
$res = mysql_query($sql)
.
.
.
mysql_free_result($res) # Does $res's content still exist in memory even though $res is out of scope ?
}
So the unused result-resource after a function call keeps building up in memory or gets overwritten on the next query ?
It depends upon the version of PHP. Some versions of PHP do stack the result sets and don't overwrite so you keep using more and more memory. Certain older versions of PHP 4 wouldn't clear connections when the PHP script ended either. The BEST coding practice with any database under PHP is to close and free the resultset as soon as you are FINISHED with it.
In the game listed in my signature, the scheduler can process anywhere from a hundred to a few thousand queries every 5 minutes. Even though we reused resultsets a lot of memory would be used at times. It wasn't until we closed each query with a mysql_free_result when we were finished with the data from that query that memory usage stopped going up.
It's just good programming practice to close and free your resultsets. The same thing on connections. Don't rely on PHP doing everything for you. It's just lazy.