Page 1 of 1
Easy way to find non-freed mysqli result sets?
Posted: Sun Jun 05, 2011 3:37 pm
by flying_circus
Is there a method to find all active mysqli result sets?
In an effort to tidy up some code, I'd like to find all active result sets so I can free them.
Is there anything like get_defined_vars() for this?
Re: Easy way to find non-freed mysqli result sets?
Posted: Sun Jun 05, 2011 11:06 pm
by Christopher
I don't think there is a way to get allocated link identifiers -- not that I know of. Is it not possible for you to track them as you connect? And why do you want to free them? PHP will close them automatically when the script ends (unless you use pconnect).
Re: Easy way to find non-freed mysqli result sets?
Posted: Mon Jun 06, 2011 12:30 am
by flying_circus
The main interest is to polish the code a little more and I always thought it was best practice, not to mention easy. I am not using persistent connections and I do understand PHP will automatically destroy them.
After I posted this, I did some very basic monitoring with get_memory_usage() and from what I can tell, either there is very little memory consumed or I don't understand what I am doing.
Code: Select all
Query: 'SELECT * FROM `sessions`;'
Memory Usage Before Query: 313224
Memory Usage After Query: 313532
Memory Usage After Free: 313496
Granted, sessions table only has about a dozen records and a few fields in it right now... which makes me wonder how much memory a larger result set might consume?
Re: Easy way to find non-freed mysqli result sets?
Posted: Mon Jun 06, 2011 2:01 am
by Christopher
The important thing is what happens after the result set is loaded. In most PHP scripts the result data is iterated through and shortly thereafter the scripts exit. So there is little need to free the memory manually. I even recall reading somewhere that it is faster to let PHP do this on script exit that in the script -- don't know if that is true though.
However, if your script load several large result sets sequentially then freeing the as you go may make sense. Do you actually have a memory problem or is this mainly Premature Optimization?
Re: Easy way to find non-freed mysqli result sets?
Posted: Tue Jun 07, 2011 2:25 pm
by flying_circus
Christopher wrote:I even recall reading somewhere that it is faster to let PHP do this on script exit that in the script -- don't know if that is true though.
Interesting, I might have to dig a little deeper
Christopher wrote:However, if your script load several large result sets sequentially then freeing the as you go may make sense. Do you actually have a memory problem or is this mainly Premature Optimization?
I don't know about premature optimization, but preventative optimization might be a better choice of words.
It's for a framework that I've been working on. Once the framework is done, I will have little control over how large or small query result sets might be, let alone how many queries each script may actually make. I suppose that will all depend on the clients needs. Anyways, that's a little off-topic. At this point, I am looking at it strictly from a performance perspective and as a "clean up" tool. I was hoping PHP might have something native, but I could always write a wrapper around the database query object for debugging.
Back Story:
I've been accumulating enough customers over the years that I am toying with the idea of getting a dedicated server and hosting my own projects. Most of my clients know little to nothing about programming, so I dont expect them to modify the scripts, but if I give them account credentials, who's to tell what they do with it. I guess I'm trying to make the switch from balancing optimization with project cost (because I'm using someone else's hardware resources), and optimizing from the get go as I'll be using my own hardware resources. Am I just spinning my wheels?