Page 1 of 1

Detect allowed memory size.

Posted: Fri Oct 15, 2010 12:27 pm
by dimxasnewfrozen
I have a php script that is querying a massive database. I'm getting errors saying "Allowed memory size X exhausted... "

I know I can use...

Code: Select all

ini_set("memory_limit","20M");
to limit it but is there a way to detect if the memory limit was reached without displaying the ugly error message?
Some kind of exception handler?

Thanks

Re: Detect allowed memory size.

Posted: Fri Oct 15, 2010 12:31 pm
by John Cartwright

Re: Detect allowed memory size.

Posted: Fri Oct 15, 2010 12:54 pm
by dimxasnewfrozen
Hmm, that is helpful but not exactly working. It may be something I have to do on the database level.

I have a database query that could possibly return over a million records. Even if I check the memory size before or after, it's still bombing DURING the query. So if a query is trying to return over a million records or over a certain memory size, I'd like to throw an exception and terminate the query if that's possible.

Re: Detect allowed memory size.

Posted: Fri Oct 15, 2010 12:59 pm
by John Cartwright
dimxasnewfrozen wrote:Hmm, that is helpful but not exactly working. It may be something I have to do on the database level.

I have a database query that could possibly return over a million records. Even if I check the memory size before or after, it's still bombing DURING the query. So if a query is trying to return over a million records or over a certain memory size, I'd like to throw an exception and terminate the query if that's possible.
If your returning a million rows, chances are your design is terribly innefficient.

Re: Detect allowed memory size.

Posted: Fri Oct 15, 2010 1:08 pm
by dimxasnewfrozen
Well, most likely. I can definitely make sure the query doesn't get executed incorrectly. However, for my own stubbornness, is it possible terminate a query if it exceeds the memory size?

Re: Detect allowed memory size.

Posted: Fri Oct 15, 2010 1:11 pm
by John Cartwright
I'm not sure what you mean, if your memory limit is reached then then the script execution is terminated. You probably just want to increase the memory size as much as your machine permits.

However, there is no reason why you should ever query 1million+ rows in a single query. You'd be far better off doing 100 queries fetching 10,000 rows (or some variant). This way, you can recycle the memory used instead of storing the entire result set.

Re: Detect allowed memory size.

Posted: Fri Oct 15, 2010 10:09 pm
by requinix
To spell it out: no, you can't "catch" an out-of-memory error. If it happens, your script dies.
a) Set the memory limit really high. Almost always a bad idea.
b) Adjust your code so that it doesn't need so much memory. Best idea, but can't fix everything.
c) Check memory usage during runtime and increase it as needed. Not such a good idea but has its uses.