Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sean_connell_93's avatar

High Memory usage when sending mails

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

0 likes
1 reply
click's avatar

I don't think it is something that can be solved by anyone here if it is something internally. I guess it keeps track of some models/data inside the mailer class which are not cleaned up.

One reason I have seen things like this happening is that if you have loggers/debuggers enabled. I have seen these memory increases when running 100 times a simple select id form user where id =1. In my case it was Sentry.

I would not send 2000+ emails in 1 command in the first place, why don't create smaller batches or push them all to the queue?

What kind of email is it? If it is not a super unique email, like a newsletter or some other generic email, doesnt your email provider have a "bulk send" option?

Please or to participate in this conversation.