Archive for December 14th, 2005

DB_DataObject’s Memory Issues

We are using DB_DataObjects as the database abstraction layer for Maintain. It’s a pretty convenient way to access databases, in most cases preventing you from writing ugly SQL strings at all. So far, so good.

However, when handling lots of database entries (in my case a couple of ten thousands), you might easily run into PHP’s memory limit. Imagine the following scenario:

$books = DB_DataObject::Factory('books');
$books->type = ‘novel’;
$books->find();
while ($books->fetch()) {
    do_something();
}

It will magically query your database for you, taking some memory for caching and, when you made your way through all of the returned records (what should actually happen at some time, considering that you have got a while clause here), it should destroy the cached information, setting the memory free that was used for it.

It does not.

Imagine the above code sample being inside another while() statement. It will start constantly leaking memory until it runs into PHP’s memory limit. In my case, the script finally took 56 Megs of space.

The solution is to run $books->free() after you’ve iterated through the records, even though the associated man page claims: “DataObjects stores result sets as a private global variable, normally this is free’ed after you have run through the results, or at the end of the request.”.

By that, I cut down the memory usage of my script to barely 3 1/2 megs. Sweet! :)

Update: Bug filed.
Update 2: The bug was fixed and a new, stable release is available now. Go ahead and upgrade :)

The second, good Espresso I found

Starbucks Coffee; CC licensed; src: http://www.flickr.com/photos/pyrotechnicphoenix/71464695/Yeah! The second, good espresso I found here in the U.S. was… – imagine that – the one at Starbucks coffee (downtown Corvallis).

Usually, these coffee chains really suck at making good coffee. For example, Seattle’s Best Coffee that’s sold all over Portland (ridiculously, a Washington coffee in an Oregon city), was really not much of an enlightenment.

However, a German friend of mine (who usually knows what he is talking about when it comes to coffee) told me that Starbucks really makes a difference. Not having a Starbucks in my home city in Germany, I was quite excited: A coffee shop chain that doesn’t suck?

And, ya, what can I say: The one-shot espresso at Starbucks rocks – even though it is served in a way too big paper cup and the clerk looks kind of strangely when you say, yes, I really only want a one-shot espresso. The price of 1,35 is still at least twice as much as in Italy, but considering that Italy is quite a few miles away, it is certainly worth the money.

Starbucks++ :)