如何在zend中清理/释放数据库查询内存?

After executing this simple code (for MySQL database) I get 1kB of memory less for each loop iteration, so after 1000'th iteration I have about 1MB memory used.

Now, if I have to loop in a long running script (about 1 000 000 iterations) I will be out of memory quickly

$_db = Zend_Db_Table::getDefaultAdapter();

$start_memory = memory_get_usage();

for ($i=0; $i<1000; $i++) {
    $update_query = "UPDATE table SET field='value'";
    $_db->query($update_query);
}

echo 'memory used: '.(memory_get_usage()-$start_memory);

Is there a way to free memory used by database query?

I tried to put update query in a function so after leaving function scope resources used by this function should be freed automaticaly:

function update($_db) {
  $sql = "UPDATE table SET field='value'";
  $_db->query($sql);
}
...
for ($i=0; $i<1000; $i++) {
    update($_db);
}

but they are not!

I'm not interested in advices like 'try updating mutliple rows in one go' ;)

Most probably you have the Zend_Db_Profiler enabled.

The database profiler stores each executed query which is very useful for debugging and optimisation but leads to rather fast memory exhaustion if you execute a huge numbers of queries.

In the example you gave, disabling the profiler should do the trick:

$_db = Zend_Db_Table::getDefaultAdapter();
$_db->getProfiler()->setEnabled(false);

$start_memory = memory_get_usage();

for ($i=0; $i<1000; $i++) {
    $update_query = "UPDATE table SET field='value'";
    $_db->query($update_query);
}

echo 'memory used: '.(memory_get_usage()-$start_memory);

When executing the same query multiple times the best way to save memory is to implement prepared statements. Your adapter is going to be using prepared statements, but since you are calling the query() method inside the loop, it's getting prepared every time. Move that outside of the loop:

$_db = Zend_Db_Table::getDefaultAdapter();
$_stm = $_db->prepare("UPDATE table SET field='?'");

for ($i=0; $i<1000; $i++) {
    $_stm->execute(array($fieldValue));
}