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...
#!/usr/bin/php
<?php
{
$folder="{mails erver.domain.co m:143}INBOX";
$mailbox="DOMAI N\\user\\user";
$authpass="pass word";
$mbox = imap_open($fold er,$mailbox,$au thpass,OP_READO NLY);
if (!$mbox)
{
print "imap_open( ) failed.\n";
exit(1);
}
$message_count= imap_num_msg($m box);
for ($i=1;$i<=$mess age_count;$i++)
{
$body = imap_body($mbox ,$i,FT_PEEK);
$outfile=fopen( $i.".mime","w") ;
if (!$outfile)
{
print "fopen() failed,\n";
exit(2);
}
fwrite($outfile ,$body);
fclose($outfile );
}
imap_close($mbo x);
}
?>
Comment