From: Allens <[EMAIL PROTECTED]>David,
I'm still very new to PHP and MySQL, but here is what I've found. Couldn't find any references to the free() function except for this at the website. On "How memory is used in MySQL", "lmost all parsing and calculating is done in a local memory store. No memory overhead is needed for small items and the normal slow memory allocation and freeing is avoided. Memory is allocated only for unexpectedly large strings (this is done with malloc() and free() ). "

From what I've read, MySQL frees memory automatically when regular function calls from the web back to the server that return data are executed. I'm going out on a limb because I'm not sure and this is a good way for me to learn as well. Couldn't find any real info on the free() function in the php manual or mysql manual nor in any of the manuals I have. I have about 50 users hitting our lone db, but no memory issues have occurred to date. Using Mac OS X Server 10.2.3 on Mac Server 533MHz 1GB of memory with MySQL 3.23.51. Hope this isn't wasted reading? :)

On Sunday, February 16, 2003, at 07:39 PM, [EMAIL PROTECTED] wrote:
Re: When to do free()?
	25667 by: David Chamberlin

From: David Chamberlin <[EMAIL PROTECTED]>
Date: Sun Feb 16, 2003 11:19:17 AM US/Eastern
Subject: Re: When to do free()?

Hmmm...still no takers on this one ....

I decided out of curiosity to start liberally adding free() calls whenever I did a db->query(). Unfortunately it seems that the free() call causes it to die. Not quite sure why, but it did. Most of the calls that I use a query() for (as opposed to getAll() or getOne()) are INSERT/UPDATE/DELETE. Is there some reason you shouldn't do a free() after one of those?

I'm still baffled. And still don't know if I should be using disconnect() at the end of each of my pages ....

Any thoughts?


David Chamberlin wrote:
I'm currently using pear DB to abstract out use of my mysql database. Everything is generally working fine, except that it seems that performance seems to degrade the more it is used, then I get my ISP to restart mysql and everything seems to be good again for a while, then it degrades, lather-rinse-repeat.
I'm currently one of the only ones using mysql at my ISP, so it seems that whatever I'm doing is causing issues. For the most part I'm not doing anything complex, queries are relatively simple and the dbases are currently pretty small.
So while thinking about the issue, one thing that occured to me is that I'm not doing a free() on the query results when I'm done with them. That's mostly because very few of the examples I've seen ever do this, so I assumed it was an optimization that generally wasn't necessary. Furthermore, I figured that after my page got loaded, the connection to the db would be terminated and the resources freed, so the extent of the "resource leak" would be pretty minimal.
Well, now I'm questioning the validity of those assumptions and I'm wondering if I need to start adding liberal use of free(), and I'm wondering to what extent it needs to be done?
So first question, for those familiar with pearDB. It seems that certainly after doing a query() I should do a free(). What about getAll() (and related question, is there much difference between doing a query() and getAll())?). The docs seem to indicate a getOne() automatically frees resources, so I don't need to do it there.
Next question is, what happens when no one refers to a result any more? Will a destructor get called that automatically frees the result? Or should I make sure to do the free before the reference is lost?
How long are resources held if you don't free them?
Finally, I assume that when you fetch data from a result that the data is copied and that it's not a reference, right? So as soon as I've done the necessary fetchRows() or whatever, I can safely free the result, right?
Any other ideas what might be causing problems for mysql and/or how to track the problems? I believe my ISP is using linux with apache, and a fairly recent PHP and mysql. I can get more details on exact versions if that helps.
Also, just as a general note, the basic format for most of my pages is:
1) connect to database
2) do some queries from PHP using pearDB
3) display results
I don't specifically call disconnect() when I'm done. Should I?
Sorry for the abundance of questions, and thanks for any help.

  Gale L. Allen Jr
  Macintosh Support Specialist

  "Remember, Love wins over all"

Reply via email to