Does mysql result mem get freed at the end of a function ?

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!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Does mysql result mem get freed at the end of a function ?

Post by anjanesh »

Hi

Its mentioned in docs about mysql_free_result that
All associated result memory is automatically freed at the end of the script's execution.
But does it get freed at the end of a function

Code: Select all

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 ?
 }
Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No, it gets freed when either A) you directly call mysql_free_result; or B) the database connection is closed; or C) The script execution stops.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

function foo1()
 {
        $res = mysql_query($sql)
        .
        .
        .
        return
 }
function foo2()
 {
        $res = mysql_query($sql)
        .
        .
        .
        return
 }

 .
 .
 .
So the unused result-resource after a function call keeps building up in memory or gets overwritten on the next query ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I believe the buffer is overwritten.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

anjanesh wrote:

Code: Select all

function foo1()
 {
        $res = mysql_query($sql)
        .
        .
        .
        return
 }
function foo2()
 {
        $res = mysql_query($sql)
        .
        .
        .
        return
 }

 .
 .
 .
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.
thewebdrivers
Forum Commoner
Posts: 41
Joined: Fri Aug 17, 2007 3:32 pm
Location: india
Contact:

Post by thewebdrivers »

best to use mysql_free_result
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

What abt mysqli ?
Is it more efficient in releasing resources ?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

You will still need to use the free_result for Mysqli if you want to efficiently release memory used by any returned resultsets from queries.
Post Reply