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?
Easy way to find non-freed mysqli result sets?
Moderator: General Moderators
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Easy way to find non-freed mysqli result sets?
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).
(#10850)
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: Easy way to find non-freed mysqli result sets?
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.
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?
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- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Easy way to find non-freed mysqli result sets?
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?
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?
(#10850)
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: Easy way to find non-freed mysqli result sets?
Interesting, I might have to dig a little deeperChristopher 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.
I don't know about premature optimization, but preventative optimization might be a better choice of words.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?
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?