Memory leak using symfony2 & swiftmailer

I use the symfony2 command as a cron job to send bulk email to site members.

Actual code:

$output->writeln('Before: '.(memory_get_usage() / 1024 / 1024));

$mailer->send($m);

$output->writeln('After: '.(memory_get_usage() / 1024 / 1024));

My results:

Before: 182.38 MB
After: 183.73 MB

Each time I send an email, the swiftmailer consumes an additional 1 MB of memory. It just doesn't seem right, but the memory grows every time a new message is sent. Am I something wrong here?

+3
source share
3 answers

: , downvoting. , swiftmailer , /. , , . , swiftmailer, .

. MailTransport swiftmailer. , .

throttlerplugin , :

class EmailThrottler {

    private $startTime;
    private $messagesPerMinute;
    private $_messages;

    public function __construct($messagesPerMinute = 25)
    {
        $this->startTime = time();
        $this->messagesPerMinute = $messagesPerMinute;
        $this->_messages = 0;
    }

    public function run()
    {
        $this->_messages++;
        $duration = time() - $this->startTime;
        $sleep = $this->_throttleMessagesPerMinute($duration);

        if ($sleep > 0) {
            sleep($sleep);
        }
    }

    private function _throttleMessagesPerMinute($duration)
    {
        $expectedDuration = $this->_messages / ($this->messagesPerMinute / 60);
        return (int) ceil($expectedDuration - $duration);
    }

}

:

$throttler = new EmailThrottler($pendingCampaign->getRateLimit());

:

$mailer->send($m);
$throttler->run();

, . , - 5.4, , 5.3 , 5.3

:)

-1

SwiftMailer

, , , . , , SwiftMailer .

send() SwiftMailer , . memory, spool flush . , memory_get_usage() , ( , , script , SwiftMailer ).

Symfony2:

, . , , - .

, File.

, :

# app/config/config.yml
swiftmailer:
    # ...
    spool: { type: memory }

:

# app/config/config.yml
swiftmailer:
    # ...
    spool:
        type: file
        path: /path/to/spool

cron :

$ php app/console swiftmailer:spool:send --env=prod

, , script. , , , . cron, , , . .

+5

, swiftmailer. , , , ( , ).

simfony2, , config:

swiftmailer:
    logging: false

:

swiftmailer:
    mailers:
        my_mailer:
            logging: false

In my case, sending ~ 1,500 letters, the memory grew to ~ 100 MB (only due to the registrar), now> 0.2 MB.

I know this a bit later, but maybe it can be useful to someone who gets into the same problem. :-)

+5
source

All Articles