How to fix the PHP fatal error: allowed memory exhausted? (From the office!)
Tags: php, memorylimit, phperror, multithreading
Published: 2022-08-02 09:22:45

---
title: How to fix the PHP fatal error: allowed memory exhausted? (From the office!)
published: true
description: Give me more memory!
tags: php, memoryLimit, phpError, Multithreading
cover_image: https://images.ctfassets.net/cnu0m8re1exe/uTkqQAbjpnnpceaY9UzLE/2d49aaee28ca31631c33a02ff89b437c/brainresearch.jpg
---

#####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:

```php
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?


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.
```shell
Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/php/script
```
or this:
```shell
PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/php/script
```
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;"
```
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
```shell
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
```php
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:
```shell
php -d memory_limit=512M composer require this/is/a/package
```
Here is another command if you are specifically getting some package from Composer.
```shell
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.
```shell
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**.

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](https://anastasionico.uk/blog)