Skip to content

A job does not release its memory after being ran #56395

@momala454

Description

@momala454

Laravel Version

12.21.0

PHP Version

8.4

Database Driver & Version

No response

Description

I have this job which fill a variable with a lot of data data

<?php

declare(strict_types=1);

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

#[\Illuminate\Container\Attributes\Scoped]
class TestBug implements \Illuminate\Contracts\Queue\ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    protected string $bla;


    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $this->bla = str_repeat('aaaaa', 10000000);
    }
}

I am monitoring the memory usage on AppServiceProvider

\Illuminate\Support\Facades\Queue::before(function (\Illuminate\Queue\Events\JobProcessing $job) {
            \Illuminate\Support\Facades\Log::info('Before ' . json_encode($job->job->payload()) . ' Memory : ' .
                        \Symfony\Component\Console\Helper\Helper::formatMemory(memory_get_usage())
                        . ' real : ' . \Symfony\Component\Console\Helper\Helper::formatMemory(memory_get_usage(true))
                        . ' peak : ' . \Symfony\Component\Console\Helper\Helper::formatMemory(memory_get_peak_usage(true)) . ' ' . gethostname());
        });

\Illuminate\Support\Facades\Queue::after(function (\Illuminate\Queue\Events\JobProcessed $job) {
            \Illuminate\Support\Facades\Log::info('After ' . json_encode($job->job->payload()) . ' Memory : ' .
                        \Symfony\Component\Console\Helper\Helper::formatMemory(memory_get_usage())
                        . ' real : ' . \Symfony\Component\Console\Helper\Helper::formatMemory(memory_get_usage(true))
                        . ' peak : ' . \Symfony\Component\Console\Helper\Helper::formatMemory(memory_get_peak_usage(true)) . ' ' . gethostname());
        });

I am running the worker using artisan queue:work --timeout 0 --tries 20
And scheduling it every second

$schedule->job(new \App\Jobs\TestBug())->everySecond()->withoutOverlapping(720)->onOneServer();

And the memory used during the job is not released

[2025-07-24 10:04:53] development.INFO: Before {"uuid":"b08f49b2-1c6b-4995-ba59-5bba6165a54a","displayName":"App\\Jobs\\TestBug","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":null,"retryUntil":null,"data":{"commandName":"App\\Jobs\\TestBug","command":"O:16:\"App\\Jobs\\TestBug\":0:{}"},"createdAt":1753351001,"delay":null} Memory : 45.3 MiB real : 49.0 MiB peak : 49.0 MiB 62f8244a68b7  

  2025-07-24 10:04:53 App\Jobs\TestBug ....................................................................................................................................................................................................................... RUNNING
[2025-07-24 10:04:53] development.INFO: After {"uuid":"b08f49b2-1c6b-4995-ba59-5bba6165a54a","displayName":"App\\Jobs\\TestBug","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":null,"retryUntil":null,"data":{"commandName":"App\\Jobs\\TestBug","command":"O:16:\"App\\Jobs\\TestBug\":0:{}"},"createdAt":1753351001,"delay":null} Memory : 94.1 MiB real : 96.7 MiB peak : 96.7 MiB 62f8244a68b7  

  2025-07-24 10:04:53 App\Jobs\TestBug .................................................................................................................................................................................................................. 21.57ms DONE
[2025-07-24 10:04:53] development.INFO: Before {"uuid":"454b175e-536c-47ee-a4a6-0f69306cba73","displayName":"App\\Jobs\\TestBug","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":null,"retryUntil":null,"data":{"commandName":"App\\Jobs\\TestBug","command":"O:16:\"App\\Jobs\\TestBug\":0:{}"},"createdAt":1753351002,"delay":null} Memory : 94.1 MiB real : 96.7 MiB peak : 96.7 MiB 62f8244a68b7  

  2025-07-24 10:04:53 App\Jobs\TestBug ....................................................................................................................................................................................................................... RUNNING
[2025-07-24 10:04:53] development.INFO: After {"uuid":"454b175e-536c-47ee-a4a6-0f69306cba73","displayName":"App\\Jobs\\TestBug","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":null,"retryUntil":null,"data":{"commandName":"App\\Jobs\\TestBug","command":"O:16:\"App\\Jobs\\TestBug\":0:{}"},"createdAt":1753351002,"delay":null} Memory : 94.1 MiB real : 96.7 MiB peak : 96.7 MiB 62f8244a68b7  

Once reached 97mb, it is never releasing it.

I even tried the attribute Scoped to prevent a singleton to being kept, but it didn't made any change.

The only way to prevent this memory leak is to unset $this->bla inside the handle method.
Unsetting bla in the destructor does not change anything. The destructor is even called before the handle is called

Steps To Reproduce

see above

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions