Laravel: “Allowed memory size exhausted” error during unit tests

I’ve been using in-memory testing for my projects based on Laravel every since I found this great  tutorial:

It was working very well as my test cases increases, to a point when I’m starting to get this error message:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16 bytes) in /Applications/AMPPS/www/project/vendor/symfony/console/Sym
fony/Component/Console/Command/Command.php on line 57

I couldn’t find the proper solution, but managed to get it working with a workaround here:

We can temporarily increase the memory limit using this function:

// Temporarily increase memory limit to 256MB

In my case, I needed more than 128MB of memory, so I conveniently increased it to 256MB. It depends on your usage and your hardware. Choose accordingly.

Since this is only required during my test (I follow the TDD approach, so testing comes regularly), I had added it in the TestCase class, inside method createApplication().

This is the full code in TestCase.php.


class TestCase extends Illuminate\Foundation\Testing\TestCase {

public function createApplication()
    // Temporarily increase memory limit to 256MB

    $unitTesting = true;
    $testEnvironment = 'testing';

    return require __DIR__.'/../../bootstrap/start.php';

 * Default preparation for each test
public function setUp()

 * Migrates the database and set the mailer to 'pretend'.
 * This will cause the tests to run quickly.
private function prepareForTests()



  • 0ffb1t

    June 6, 2014

    A big ‘thank you’ for your tip. I’ve run into the memory limitation when trying to migrate/import data from an old app.

  • sundeeprana

    July 31, 2014

    I ran into a similar issue and I was able to fix it by disabling query logging – DB::disableQueryLog(); Unless you will be reviewing those later on, it’s kinda useless but eats up a lot of memory, especially when running recursive commands.

  • Varcos

    March 24, 2016

    Another possibility is to just add the memory on the command line: php -d memory_limit=512M artisan do:tests

  • zer0pants

    October 18, 2017

    Another option:

    run $phpunit tests –process-isolation

Comments are closed.