$wpdb->get_results leaking memory

I have developed a wordpress plugin that reads large custom xml files and imports the information as custom post types and a lot of post meta information. So I am calling a lot of database transactions. The plugin runs for several minutes within one POST’ed process and then aborts with out of memory (250 MB).

Tracking the problem down to a minimum I have a minimal example that shows the problem. This example is leaking about 18 kbyte and this is summing heavily up during runtime of the plugin. The query checks if a post with a given title exists ($title)

echo "<p>memory: " . memory_get_usage() . "</p>";
global $wpdb;
$poststable = $wpdb->prefix."posts";
$items = $wpdb->get_results("SELECT ID FROM $poststable WHERE post_title='$title'");
unset($items);
$wpdb->flush();
echo "<p>memory: " . memory_get_usage() . "</p>";

The output is
memory: 80777008
memory: 80795376
so the difference is about 18 kbyte. Where are those bytes? Although this is a small amount of memory it is summing up to several hundred megabytes.

Thanks!
Chris

Solutions Collecting From Web of "$wpdb->get_results leaking memory"