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

ryanmortier's avatar

Allowed memory size of 536870912 bytes exhausted

I just upgraded my homestead to the latest version and now a script I've been running successfully is now getting the error:

PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /home/vagrant/Code/app1/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 458


  [Symfony\Component\Debug\Exception\FatalErrorException]
  Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes)

The previous version of homestead worked fine and my production server works fine. Why am I suddenly getting this error?

I know I can just up the memory limit of PHP but I am trying to avoid that if I can because it was working fine before. Current memory limit is set to 512MB.

Here is the script I'm running, is there anything wrong with it?

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Metric;
use App\Location;
use League\Csv\Reader;
use League\Csv\Statement;

class ImportMetrics extends Command
{
    protected $signature = 'import:metrics';

    protected $description = 'Import metrics from CSV';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        if (file_exists($path = storage_path('app/import.csv'))) {
            Metric::truncate();

            $locations = Location::all();

            $csv = Reader::createFromPath($path);
            $records = (new Statement())->offset(1)->process($csv);

            $bar = $this->output->createProgressBar(count($records));

            foreach ($records as $record) {
                $record = array_map('filter', $record);

                $metric = new Metric([
                    'product_code' => $record[7],
                    'product_group' => $record[3],
                    'product_subgroup' => $record[4],
                    'product_class' => $record[5],
                    'product_subclass' => $record[6],
                    'unit' => $record[11],
                    'previous_quantity' => number($record[16]),
                    'previous_mt' => number($record[39]),
                    'previous_50lb' => number($record[37]),
                    'previous_sales' => number($record[15]),
                    'previous_gp' => number($record[36]),
                    'current_quantity' => number($record[10]),
                    'current_mt' => number($record[40]),
                    'current_50lb' => number($record[38]),
                    'current_sales' => number($record[9]),
                    'current_gp' => number($record[30]),
                ]);

                $metric->location()->associate(
                    $locations->where('code', $record[0])->first()
                );

                $metric->save();

                $bar->advance();
            }

            $bar->finish();
        }
    }
}
0 likes
4 replies
lostdreamer_nl's avatar

There doesnt seem to be any problem with your script (depending on how big that CSV file is).

I think that, either the CSV file is so big that loading it in memory at once is challenging with 512MB, or the DB query log is growing too big (in addition to the csv file in memory).

Try disabling the query log during this import, it might just push you back to the right side of your memory limit: Simply add:

\DB::disableQueryLog();

within the handle() block.

ryanmortier's avatar

Didn't work unfortunately.

I've tried so many things. I've completely re-written it without using Leage\CSV as well and just streaming the CSV with PHP's built in functions and the same problem. I give up, ini_set('memory_limit', '2048M'); wins.

3 likes
anuzpandey's avatar

@ryanmortier I tried changing it in php.ini no success but adding ini_set('memory_limit', '2048M'); in index.php fixed it.

Please or to participate in this conversation.