Wise Monks

I've got memory problem with my code.
But first descritpion:
I have mbox make by bcc_all with mails. I need sort this mbox to mboxes every user in our domain, i.e:
if user abc@mydomain.com send to somebody - mail should go to abc mbox.
if user xxx@yahoo.com send mail to bcd@mydomain.com - mail should go to bcd mbox.
if user abc@mydomain.com send mail to bcd@mydomain.com, cde@mydomain.com and yyy@yahoo.com mail should go to mboxes abc, bcd, cde.
if we have some mails not from/to users from our domains - we don't do anything.

This is my code:
#!/usr/bin/perl use strict; use warnings; use File::Basename; use Mail::Box::Manager; ### configuration ### my @our_domain = qw /mydomain.com mail.mydomain.com/; my $copymail = '/home/uksza/m/copymails'; ################### ### subs ### sub compress; #zip mail to zip-malibox sub change_name; #add date to name ############ my $mbm = Mail::Box::Manager->new; my $mbm_do = Mail::Box::Manager->new; my $mbox_cm = $mbm->open( folder => $copymail, access => 'rw' ) or die "Can't open $!"; my @all = $mbox_cm->messages; my $counter; foreach my $mail (@all) { #print for tests print $counter++ . "/$#all\n"; my @senders = $mail->from; my @recipients = $mail->destinations; #@senders = push @senders, @recipients; # - DOES'N WORK :-( save( $mail, @senders ); save( $mail, @recipients ); } #compress rest in start mbox $mbm->close($mbox_cm) or die "Can't close $mbox_cm"; compress( basename($copymail) ); ######################################### sub save { my $mail = shift; my @persons = @_; foreach my $person (@persons) { #users without hostname next if ( !$person->host ); #user not from our domains next unless ( grep { $person->host eq $_ } @our_domain ); my $name = lc $person->user; my $mails_to = $mbm_do->open( folder => "./$name", create => 1, access => ' +a' ) or die "Can't work on mbox $name. $!"; $mbm->moveMessage( $mails_to, $mail ) or die "Moving problem $ +!"; $mbm_do->close($mails_to) or die "Close problem $!"; compress($name); } } #for archive on CD sub compress { my $box = shift; my $wyj = `gzip -c "$box" >> "$box.gz"`; change_name("$box.gz") if @{ [ stat("$box.gz") ] }[7] > 1024 * 102 +5*640 ; unlink $box or die "Unlink error. $!"; } #add _rrr-mm-dd to file name sub change_name { my $name = shift; my $year = @{ [localtime] }[5] + 1900; my $month = @{ [localtime] }[4] + 1; my $day = @{ [localtime] }[3]; my $data = sprintf "_%4d-%02d-%02d.gz", $year, $month, $day; my $new_name = $name. $data; rename $name, $new_name; }
Generally it works good (litle problem with bad spam mailadresses, but who cares), but it eat tons of my memory like kid icecreams (on bcc_mbox about 180MB (1300 mails) 256MB ram and 500mb swap is sometimes too less).
Can you show me where I made mistake?

Thanx, brothers and sisters,
Yours Lukasz

In reply to Memory problem by uksza

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.