Hi, I've an issue with one of our crons that wakes up once a day to send around 2000 to 6000 emails. We've started to run into an issue with the memory maxing out.
E.g (Fatal error: Allowed memory size of X bytes exhausted (tried to allocate X bytes))
I've narrowed down the issue to the Mail::send() method and had recorded the amount of memory it was using before and after sending the mail and noticed that it wasn't freeing up memory leading to a memory leak.
I was able to prove that the issue was coming from the Mail::send() by creating a test cron class that sent 2000 mails. I set the mail driver to "log" so that it just logs them instead of sending 2000 emails
// config/mail.php
'driver' => 'log'
In the test script I've echoed the memory usage after each time the Mail::send() is called and I can see it increasing for each iteration.
<?php
// app/Services/TestCron.php
namespace App\Services;
use Illuminate\Mail\Message;
use Mail;
class TestCron
{
public function __construct()
{
foreach (range(0, 2000) as $index) {
Mail::send('test-email', [], function (Message $message) {
$message->from('[email protected]', 'From');
$message->to('[email protected]', 'To');
});
$using = sprintf('%sMb', round(memory_get_usage() / (1024 * 1024), 4));
echo sprintf('Memory Leak Debug - using [%s]', $using) . PHP_EOL;
}
dd('Done');
}
}
Inside the blade thats being rendered is just a simple bit of html and an echo of the current date
// resource/views/test-email.blade.php
<h1>Testing {{ date('Y-m-d') }}</h1>
If I open thinker php artisan tinker and boot the class app('App\Services\TestCron') I get the following out put.
I can see the memory goes from 35.8857Mb to 60.0458Mb which is almost double from what it started with.
Does anyone know why this happens? Is it an issue with the Laravel Mail::send() or is it not designed to send mails in this way. I was looking at the idea of a queue or batching them in some way but I can't understand why the memory is not being freed up after each iteration
Memory Leak Debug - using [35.8857Mb]
Memory Leak Debug - using [35.8995Mb]
Memory Leak Debug - using [35.9122Mb]
Memory Leak Debug - using [35.9249Mb]
Memory Leak Debug - using [35.9376Mb]
...
Memory Leak Debug - using [59.964Mb]
Memory Leak Debug - using [59.9757Mb]
Memory Leak Debug - using [59.9874Mb]
Memory Leak Debug - using [59.9991Mb]
Memory Leak Debug - using [60.0108Mb]
Memory Leak Debug - using [60.0224Mb]
Memory Leak Debug - using [60.0341Mb]
Memory Leak Debug - using [60.0458Mb]
PHP version 7.3
Laravel version v8.83.27