barryvdh / laravel-snappy

Laravel Snappy PDF
MIT License
2.64k stars 292 forks source link

Memory leak #536

Open TDKhang opened 3 months ago

TDKhang commented 3 months ago

I have many queue, each queue will export 1 pdf file
Sometimes I get error in laravel log

Allowed memory size of 134217728 bytes exhausted ...

After investigation it seems the cause is memory leak
I tried to reproduce it as follows

Environment

Queue job

<?php

namespace App\Jobs;

use Barryvdh\Snappy\Facades\SnappyPdf;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class DummyJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     */
    public function __construct()
    {
        $this->onConnection('database');
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        Log::info('--------------------------------');
        Log::info('Start: '.(memory_get_usage() / 1024));
        $snappy = SnappyPdf::loadHTML('<div>Something</div>');
        // unset($snappy);
        // gc_collect_cycles();
    }
}

Start memory is continuously increasing until it exceeds the limit

[2024-08-19 19:45:02] local.INFO: --------------------------------  
[2024-08-19 19:45:02] local.INFO: Start: 8472.5234375  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 8933.140625  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 8945.71875  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 8958.296875  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 8970.875  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 8983.453125  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 8996.1875  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9008.765625  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9021.34375  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9033.921875  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9046.5  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9059.078125  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9071.65625  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9084.234375  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9097.125  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9109.703125  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9122.28125  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9134.859375  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9147.4375  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9160.015625  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9172.59375  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9185.171875  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9197.75  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9210.328125  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9222.90625  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9235.484375  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9248.0625  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9260.640625  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9273.21875  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9285.796875  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9299  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9311.578125  
[2024-08-19 19:45:03] local.INFO: --------------------------------  
[2024-08-19 19:45:03] local.INFO: Start: 9324.15625  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9336.734375  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9349.3125  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9361.890625  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9374.46875  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9387.046875  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9399.625  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9412.203125  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9424.78125  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9437.359375  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9449.9375  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9462.515625  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9475.09375  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9487.671875  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9500.25  
[2024-08-19 19:45:04] local.INFO: --------------------------------  
[2024-08-19 19:45:04] local.INFO: Start: 9512.828125  

I tried unset or gc_collect_cycles, but it didn't fix it laravel-2024-08-19-unset.log laravel-2024-08-19-gc.log