Tuesday, March 25, 2008

I write DB scripts in PHP

Lots of people like PERL, Python, Java or C to write database backfills, data repair, etc. I like writing in all these languages but what I don't like doing is writing the same code over and over again, so I write my DB code in the same language as the environment that I'm using. This means I write backend DB scripts in PHP, I get to reuse common DB paths, classes and functions and improve things when I see them.

But, PHP if not written correctly will use up 2GB of memory easily from loosing reference to arrays or setting globals and forgetting about them; stuff like that. This is especially visible in long running applications.

PEAR is not immune to these memory leaks. So, to get around reference problems in PEAR I do

$skiptrace =& PEAR::getStaticProperty('PEAR_Error', 'skiptrace');

$skiptrace = false;

This prevents this error
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 76 bytes) in /usr/share/pear/PEAR.php on line 872

from long running processes.

Thought I pass it along.


Evert said...

So this is PHP4?

Mike Brittain said...

Amen, brother. I love this sort of re-use. I've found myself using PHP for more and more of my command-line scripts instead of rewriting classes into Perl.

bobuss said...

In fact, it is due to object references in the stack trace generated by the debug_backtrace() function. So we get some nice circular references between objects, that the garbage collector cannot handle (I am talking about PHP >5).
see this bug

Dathan said...

@evert: PHP5

Anonymous said...

This looks like a bug in PEAR, doesn't it ?