Edit: Not sure why users are downvoting. The fact is that swiftmailer itself has a memory leak directly related to its events/plugin system. When hooks are executed it eats away memory which never gets released. Solely removing the events system alone fixed the memory leak without breaking the way swiftmailer works when it comes to actually sending email.
I fixed the problem. I edited the MailTransport class and removed the events system in swiftmailer. I am only allowing the message itself to be built and sent, none of the before and after plugin crap.
I took the pieces from the throttlerplugin and created by own version of it which specifically will only handle messages per minute:
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);
}
}
I initialized my custom throttler class before the mailing loop:
$throttler = new EmailThrottler($pendingCampaign->getRateLimit());
and I run it after every sent email:
$mailer->send($m);
$throttler->run();
Hopefully they find a fix to their events system. I am going to see if the performance is any different on 5.4 anyway, but to those who are on 5.3 this solution I just detailed worked for me on 5.3
Cheers :)