How to fix the PHP fatal error: allowed memory exhausted?

Coding (PHP 8)

Aug 2, 2022

5 easy methods to add more memory and solve this issue
 
/img/blog/how-to-fix-the-php-fatal-error-allowed-memory-exhausted.jpeg

How does PHP work?

 

Johnny is a newcomer to the world of web development, he found a dusty old PHP manual in his basement and started playing around with it.

 

While doing some experiments and learning about loops he wrote this:

 

while (true) {
    echo "I am learning php";
}

 

His laptop burned and he stopped coding forever.

 

End of the story! bye.

 

Well, that would be too easy of a post for me to write so let’s actually write some more.

 

PHP has been around for quite some time and it is a very smart language.

 

It is so smart that it will actually prevent your laptop to burn.

 

Even if you are just started and you keep writing infinite loops like the one above.

 

The reason this will not happen is that PHP has a setting that controls the amount of memory each script is allowed to use.

 

Hayden James in his blog post says that PHP memory_limit is per-script, just as a highway’s speed limit is per-vehicle.

 

Once the usage memory of a given script reaches its limits, the processor stop, and an error is thrown.

 

Saving you from buying a new laptop.

 

One concept to specify is that PHP does not support multithreading.

 

Multithreading is a technique by which a single set of code can be used by several processors at different stages of execution.

 

That means that if you are a PHP developer you have a single processor to work with for each script.

 

 

 

 

Why do you get this error?

 

Aas mentioned PHP allocates a given amount of memory to each script that is running.

 

When your code surpasses the limit, PHP stops.

 

It is a way to prevent the script to run forever, you damage the hardware or you deploying bad-performance code to a production server (that might be costly).

 

By default, only 128 megabytes are allowed for any PHP script and it should be fairly easy to stay within the limit. but in some cases, you loop way too many elements or require a very big package from Composer and one of the following can happen.

 

Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/php/script

 

or this:

 

PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/php/script

 

so

 

what do we do then?

 

How to fix it?

 

There are a few solutions available, the most common one is to increase the memory limit allowed for your PHP script.

 

The first thing first you need to understand though is if you have to fix your code instead.

 

Just Imagine if your script can use up to 20GB and you still run into this error.

 

Unless you’re managing data for the whole userbase of Facebook.com this is a signal that there is something terribly wrong in your code.

 

 

Step #1 Check how much memory your script is allowed to use

 

php -r "echo ini_get('memory_limit').PHP_EOL;"

 

The command above needs to be copied and pasted into your terminal and it will show you how much memory is allowed at this given moment.

 

If it is a reasonable amount and you run out of it, then you should start thinking about increasing your memory limit.

 

Step #2 Add more memory

 

There are many ways you can increase the limit of your memory.

 

The best one is probably by updating the php.ini file

 

This file contains a whole list of setting PHP uses.

 

Find the one called memory_limit and give it an explicit value, for example, 2G

 

memory_limit = 2G

 

You can also update this setting from the code itself by using the php_ini function at the beginning of your PHP file

 

ini_set('memory_limit','512M');

 

In case you’re running some script via the command line you can also increase the limit straight from there.

 

Here is how:

 

php -d memory_limit=512M composer require this/is/a/package

 

Here is another command if you are specifically getting some package from Composer.

 

COMPOSER_MEMORY_LIMIT=1G composer require this/is/a/package

 

You might have noticed that I always specify the amount of memory I am allocating.

 

Another trick, to be used sparingly is to give to the value a negative value.

 

What this does is to set an infinite (better say the full amount of memory available on your machine) value of memory to each script.

 

memory_limit = -1

 

What is your thought?

 

Dealing with memory has been a pain for any PHP developer, if you never encountered this error I can assure you that it is just a matter of time before it happens.

 
 
If you like this content and you are hungry for some more join the Facebook's community in which we share info and news just like this one!

Other posts that might interest you

Coding (PHP 8) Nov 13, 2022

The New Match Expressions in PHP

See details
Coding (PHP 8) Nov 16, 2022

This is why Symfony 6 is perfect for your next project (PHP Framework)

See details
Coding (PHP 8) Nov 18, 2022

The Discovery Phase of the Web Design Process

See details
Get my free books' review to improve your skill now!
I don't want to improve