views:

268

answers:

6

I just saw this in a script

mysql_free_result($rawdb);

I saw it on a script somewhere, it ran a mysql query and saved the results to an array, right after adding to the array, it ran that which I assume free's the memory used from the mysql query.

I am just asking to verify, is it a good idea to do this? I have a couple mysql queries which return a large result which is saved to an array, should I run that code after it?

Also in a performance conscious environment is it good practice to unset large sessions and variables when finished with them?

+2  A: 

I wouldn't worry about it. All the result resources are freed at the end of the script, and unless you're having problems with memory when doing queries with large result sets, the difference will be negligible.

That said, if I were reading your code and saw mysql_free_result i'd know for sure that you weren't going to be using that resource anywhere further on in the code, so it could add a little readability...

nickf
A: 

From the manual:

mysql_free_result() only needs to be called if you are concerned about how much memory is being used for queries that return large result sets. All associated result memory is automatically freed at the end of the script's execution.

karim79
A: 

The PHP manual page for the function answers most of your questions:

http://us.php.net/manual/en/function.mysql-free-result.php

Amber
A: 

There's some discussion of this in the PHP API documentation. Depending on your use case (i.e., the size of the results you're retrieving), mysql_free_result() may improve or decrease performance. As the commenter at the bottom of that link says, call memory_get_usage() to get an idea of whether you should free the result or not. Nothing beats actually examining your resource usage.

Meredith L. Patterson
thanks I just saw that, interesting, kinda sucks
jasondavis
+1  A: 

I would think it good practice to put into your data abstraction layer, as it makes it a good citizen with memory handling. Especially as a lot of PHP code is not. :-/

The issue of memory usage was a whole lot worse in PHP 4 than in 5, but it can still be an issue. If you have to keep raising the maximum memory allowed to your PHP script (the default is 8Mb, but most environments I use have this up at 64Mb), then you probably should be thinking about how your script is using and over-using memory. Using mysql_free_result() is one piece in this arsenal, but on its own is rather pointless. Processing huge datasets can be done much more memory efficiently if you use mysql_unbuffered_query() and process each row as you retrieve it with mysql_fetch(). I've seen the capacity of scripts to process data go up enormously when re-written to use this approach.

staticsan
+1  A: 

Also in a performance conscious environment is it good practice to unset large sessions and variables when finished with them?

Note that if you unset variables from $_SESSION, you will effectivly delete them from the session : they won't be there the next time your user calls a page of your website.

So, you might want to be careful with that idea : only delete data from $_SESSION when you don't need it anymore.

About unsetting data and/or using functions like mysql_free_result : considering your scripts are (I suppose, as PHP is for web-development, and users won't wait hours for pages to load) running only for a few hundred milliseconds, freeing memory that way is probably a bit overkill : as long as you don't get memory_limit-related errors, you probably shouldn't care.

Pascal MARTIN