DEV Community

Cover image for How to fix the PHP fatal error: allowed memory exhausted? (From the office!)
anastasionico
anastasionico

Posted on

How to fix the PHP fatal error: allowed memory exhausted? (From the office!)

Give me more memory!

Hi, I am Nico,

I am a Senior Engineer for a ticket broker based in London.

From the office! is a daily (almost) posting routine that I keep to journal about the tech things I have been doing in my day-to-day office life.

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";
}
Enter fullscreen mode Exit fullscreen mode

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?

As 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
Enter fullscreen mode Exit fullscreen mode

or this:

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

sooo

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;"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

COMPOSER_MEMORY_LIMIT=1G composer require this/is/a/package
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Have you already gotten this error?

Which techniques have you used to solve it?

About me

I have been a web developer for almost a decade now.

Working with many businesses and award-winning marketing agencies situated in the heart of London.

Also, I write articles and tutorials on my blog and online communities and help businesses build their presence online.

Click here to read more than 100+ of my blog post

Discussion (7)

Collapse
marcusatlocalhost profile image
Marcus

you have to fix your code instead.

This is the best advice!

Trying to pull 50000 records and loop through them and then running out of memory should make you reconsider things - like use of unbuffered queries and php generators

Collapse
anastasionico profile image
anastasionico Author

how often do you use generators?

Collapse
marcusatlocalhost profile image
Marcus

I use generators rarely, because the use cases are limited. This comment described it quite well: https://www.reddit.com/r/PHP/comments/wgu97y/comment/ij25qgi/?utm_source=reddit&utm_medium=web2x&context=3
There are only so many cases where one is "streaming" and processing content. Like reading a log file or CSV file. In those cases, the performance/memory consumption was notable.

Thread Thread
anastasionico profile image
anastasionico Author

yes, I agree with you they are very good in what they do but they are very...'niche'.

I also like array functions (like array_map, array_filters etc) and love the Illumitante's Collections.
have you ever use it, it's lifechanging?

Thread Thread
marcusatlocalhost profile image
Marcus

Yeah Laravel Collections are awesome (I read your blog post about the book "Refactoring Collection" too, I didn't read the book yet, because I already know about the power of collections :))

I even used collection.js in a project because I memorized most methods anyway and it was easy to port certain code.

Thread Thread
anastasionico profile image
anastasionico Author

oh great thank you so much

Collapse
anastasionico profile image
anastasionico Author

What solution is your favourite?