Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm writing a php program to extract emails from a microsoft exchange
server and write them to files on my linux box. I'm having a problem
which looks like a memory leak in imap_body(). But I find it hard to
believe that nobody has noticed it before, so I am probably doing
something wrong.

If I watch the program using top, the SIZE and RSS rapidly increase
until the program hits the size limit. I can workaround the problem by
moving the calls to imap_open() and imap_close() inside the loop. The
imap close frees the memory that imap_body allocated, but it's very
slow and inefficient to do it that way.

If I comment out the call to imap_body() then the memory usage does not
increase, so it's definitely that line causing the problem.

I am currently using php as supplied with Fedora1. I will upgrade to a
newer version
and see if that helps.

Here's the code, expert advice please...

        $mbox = imap_open($folder,$mailbox,$authpass,OP_READONLY);
        if (!$mbox)
                print "imap_open() failed.\n";
        for ($i=1;$i<=$message_count;$i++)
                $body = imap_body($mbox,$i,FT_PEEK);
                if (!$outfile)
                        print "fopen() failed,\n";

Re: imap_body() wrote:
Quoted text here. Click to load it

You are storing the output of imap_body() into a variable, which may be the
cause of your problem (with large emails).

Quoted text here. Click to load it

Try to do it like this:

if (!$outfile = @fopen($i.".mime","w")) {
    print "fopen() failed,\n";
fwrite($outfile, imap_body($mbox, $i, FT_PEEK));


Re: imap_body()

Hi JW,
Thanks for the suggestions. I've followed you advice, but the problem
remains. The memory usage increases with every call to imap_body(). I
guess is really is a problem with PHP rather than with my code.

I've now upgraded PHP to 4.3.8-1.1, which is the latest rpms released
by RedHat for Fedora 1. The only option I can think of is to download
and compile the very latest version, but I'm not hopefull because I
don't see anything in the changelog relating to this problem.

Thanks again.


Site Timeline