Possible serious memory leak in MDB2

From: Date: Tue, 24 Apr 2007 18:00:28 +0000
Subject: Possible serious memory leak in MDB2
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-27135@lists.php.net to get a copy of this message
Hi, I'm having a serious memory problem that maybe part of MDB2, or perhaps PHP itself. I'm posting it here in the hope that somebody will help me to find where the problem is so I can report the bug, possibly with a fix by myself. I'm using PHP to run a CLI application. It's a script run by cron that parses some HTML files (with DOM), and I ended up using PHP to integrate with the rest of the code that already runs the website. What the application does is very simple: it opens an IMAP connection, and for each email, it parses the HTML body to extract some information out of it, and saves this information into a database. THe HTML files are less than 1MB, and number of messages read is small (< 20). Since the information is parsed by pieces, the memory used by it should peak at 10kb or 20kb. The parsing is done using DOM and xpath queries. The parsing is done in a separate method, so I was expecting that any memory allocated for parsing a message would be freed before the next one is parsed. I'm using php 5.2.0. The problem is: it's eating more memory than a black hole. It eats the current limit of 256MB set in php.ini, in an application that would hardly consume 4MB if written in C. I tried to find what the problem was sprinkling memory_get_usage() around, and the memory is apparently being lost by the INSERT query. prepare/execute leak *a lot* of memory, while a simple query() still leaks, but much less. Thinking it might be related to this bug: http://bugs.php.net/bug.php?id=39885 (memory leak when doing loads of INSERT's and there are duplicate key errors), I added a SELECT to avoid the duplicate key errors. This had an extraordinary effect: memory consumption started to decrease, and soon I was using negative memory. When the program ended a sample run, memory_get_usage() was returning '-9415900'. But even if there are no duplicate keys, memory leak occurs. Since this universe doesn't allow negative memory usage, I wonder WTF is going on. I'm using MDB2 (1.4.) as DB frontend, BTW, which may be the culprit here. What frees more memory than it allocated is a call to query('SELECT ...'). Removing this call leads back to the endless growing memory problem. Any ideas to find out what is causing this: mysql (unlikely, since it wouldn't show in PHP like this), php, mdb2? To those interested, I also posted this problem to comp.lang.php. Thanks for any help or tips.

« previous php.pear.general (#27135) next »