Fatal error allowed memory size of 1610612736 bytes exhausted tried to allocate 20480 bytes

Hi, I am using composer on my Laravel 6 project. However, somehow I always run into the problem is "Allowed memory size of 1610612736 bytes exhausted". I don't know why but since a fe...

Hi,

I am using composer on my Laravel 6 project. However, somehow I always run into the problem is «Allowed memory size of 1610612736 bytes exhausted». I don’t know why but since a few days I have this problem and I don’t know why or where it does come from.

The curios thing is that I get exactly the same error on my development server. That means it must has to do something with the Laravel project itself. Because as I said, on my Mac as well as on my development server I get the exact same output with exact the same config and project.

The only workaround is php -d memory_limit=-1 composer update. But I cannot do this on my server because I am not the admin of the server and have a limited RAM usage. So, I do want to know what is causing this problem and how I can solve this.

Do you need any more information then those below?
I appreciate any kind of help!

Kind regards and thank you!

My composer.json:

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "repositories": [
        {
            "type": "composer",
            "url": "https://nova.laravel.com"
        }
    ],
    "require": {
        "php": "^7.2",
        "components/jquery": "^3.4",
        "cybercog/laravel-nova-ban": "^1.1",
        "emilianotisato/nova-tinymce": "^1.1",
        "fideloper/proxy": "^4.0",
        "glorand/laravel-model-settings": "^3.5",
        "inspheric/nova-indicator-field": "^1.43",
        "kabbouchi/nova-logs-tool": "^0.2.0",
        "laravel/framework": "^6.2",
        "laravel/nova": "~2.0",
        "laravel/socialite": "^4.4",
        "laravel/telescope": "^3.0",
        "laravel/tinker": "^1.0",
        "laravel/ui": "^1.1",
        "laravelcollective/html": "^6.1",
        "llaski/nova-scheduled-jobs": "^3.0",
        "mad-web/nova-telescope-link": "^3.0",
        "orangehill/iseed": "^2.6",
        "paquettg/php-html-parser": "^2.1",
        "pdewit/nova-external-url": "^1.0",
        "phpunit/php-code-coverage": "^9.1",
        "phpunit/phpunit": "^9.2",
        "spatie/laravel-honeypot": "^1.4",
        "spatie/laravel-medialibrary": "^7.19",
        "spatie/laravel-permission": "^3.2",
        "twbs/bootstrap": "^4.3",
        "vyuldashev/nova-permission": "^2.9",
        "yoeunes/toastr": "^1.2"
    },
    "require-dev": {
        "barryvdh/laravel-debugbar": "^3.2",
        "barryvdh/laravel-ide-helper": "^2.6",
        "brianium/paratest": "^4.1",
        "facade/ignition": "^1.4",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^3.0"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "files": [
            "app/Helpers/helper.php",
            "app/Helpers/commentsHelper.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\Foundation\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r "file_exists('.env') || copy('.env.example', '.env');""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ],
        "post-update-cmd": [
            "Illuminate\Foundation\ComposerScripts::postUpdate",
            "@php artisan ide-helper:generate",
            "@php artisan ide-helper:meta"
        ]
    }
}

Output of composer diagnose:

Checking composer.json: OK
Checking platform settings: OK
Checking git settings: OK
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK
Checking pubkeys:
Tags Public Key Fingerprint: 57815BA2 7E54DC31 7ECC7CC5 573090D0  87719BA6 8F3BB723 4E5D42D0 84A14642
Dev Public Key Fingerprint: 4AC45767 E5EC2265 2F0C1167 CBBB8A2B  0C708369 153E328C AD90147D AFE50952
OK
Checking composer version: OK
Composer version: 1.10.10
PHP version: 7.3.18
PHP binary path: /usr/local/Cellar/php@7.3/7.3.18_1/bin/php
OpenSSL version: OpenSSL 1.1.1g  21 Apr 2020

When I run this command:

(Basically any commands where I install, remove or update packages)

composer update
composer install
...
...

I get the following output:

PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.10.7/bin/composer/src/Composer/DependencyResolver/Solver.php on line 223

Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.10.7/bin/composer/src/Composer/DependencyResolver/Solver.php on line 223

Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.

Anyone who has used Composer for more than a hot second has likely run into the dreaded “out of memory” error. There are a few ways to fix this, including a permanent fix. Let me show you how!

The Problem

The error looks something like this:

PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.9.3/bin/composer/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52
Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/Cellar/composer/1.9.3/bin/composer/src/Composer/DependencyResolver/RuleWatchGraph.php on line 52
Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.

This just happened to me while running the following command:

$ composer create-project acquia/lightning-project MY_PROJECT

Remember that Composer is using PHP to execute, so in this case the problem is that my computer most likely doesn’t have enough memory to allocate to the PHP process.

Checking PHP Memory Limit

All you have to do to check your PHP Memory Limit is:

1. figure out which php.ini file your computer is currently using (if you followed my guide on setting up a new Macbook and/or are using Homebrew, you likely aren’t using the stock one that shipped with your computer). Run this: $ php —ini

Configuration File (php.ini) Path: /usr/local/etc/php/7.3
Loaded Configuration File:         /usr/local/etc/php/7.3/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.3/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.3/conf.d/ext-opcache.ini

You’re interested in the “loaded” configuration file (so for me, it’s in /usr/local/etc/php/7.3/php.ini).

2. Using your preferred editing method, open up that php.ini file and find the memory_limit setting. This is what I found:

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 128MB

That’s not NEARLY enough!

Upping Your PHP Memory Limit

1. Using the same steps as above, we’ll want to edit the php.ini file.

Update the default memory_limit. I upped mine to 2048MB (2GB)

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 2048MB

Note: you may have to up it even higher or set it to -1 (for unlimited, which is what the Composer docs suggest).

2. Restart your apache server and terminal so the change will take effect.

sudo apachectl restart

3. Run your composer command again.

A Temporary Fix

You can also fix this on a “case by case” basis if you so desire (or your machine is locked down and you can’t change your php.ini file without an administrator’s assistance) by running commands like this:

$ COMPOSER_MEMORY_LIMIT=-1 composer <command>

This will ensure that there is no PHP memory limit imposed on the current command being run. It’s not a long term fix though, so if you don’t up the memory limit permanently you will likely have to do this over and over again!

Related Content

A recent attempt to run an update composer (regular activity for many of us), I had a memory limit issue.  This was surprising because the memory setting via Plesk is set to 2G.  Yet through Terminal it was showing only 128MB.  What gives??  

The error I was seeing:

PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted (tried to allocate 32 bytes) in phar:///usr/lib64/plesk-9.0/composer.phar/src/Composer/DependencyResolver/Pool.php on line 339

Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 32 bytes) in phar:///usr/lib64/plesk-9.0/composer.phar/src/Composer/DependencyResolver/Pool.php on line 339

Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.

or

PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/psa/var/modules/composer/composer.phar/src/Composer/DependencyResolver/Solver.php on line 223

Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///usr/local/psa/var/modules/composer/composer.phar/src/Composer/DependencyResolver/Solver.php on line 223

Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.

I have had both of these errors (at different times).  However, the solution for me was the same in both instances.

How to investigate this error

In this instance, the PHP memory_limit was being applied to the wrong PHP version.  As noted above the respective PHP version is showing 128MB and needs to be increased to something bigger.  How do you get the current memory_limit value?  By running the following command through shell program like Terminal:

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

This will confirm the current memory_limit as noted in the error being

128M

Even though Plesk had a different memory limit, composer wasn’t utilising that, but rather the setting in the php.ini file.  Subsequently, try increasing the limit in your php.ini file.  For me the php.ini is located in /etc directory.  However, it might not be in the etc directory or it is, yet it is not the php.ini file being use.  

How to find the php.ini in use?  In shell run the command:

php -i

This will output the phpinfo() data… displaying something like

PHP Version => 7.3.25

Build Date => Nov 26 2020 20:28:14

Configure Command =>  './configure'  '--docdir=/opt/plesk...'

Do a search for php.ini to see the result being something like:

Configuration File (php.ini) Path => /etc

or

Configuration File (php.ini) Path => /opt/plesk/php/7.3/etc

Loaded Configuration File => /opt/plesk/php/7.3/etc/php.ini

Resolving the error

Now that you know the php.ini file being used (for me it was /opt/plesk/php/7.3/etc), you can edit the php.ini file through using the vi command either going to the directory path and using:

vi php.ini

or 

vi /opt/plesk/php/7.3/etc/php.ini

While in edit mode, perform a quick search rather than scrolling through the file using /{search term}.  In this instance search for memory_limit, subsequently the find entry will be

/memory_limit

This will take you to the location of memory_limit, looking something like

; Maximum amount of memory a script may consume (128MB)
; http://www.php.net/manual/en/ini.core.php#ini.memory-limit
memory_limit = 128M

Use -1 for unlimited or define an explicit value like 2G (remember to enter edit / insert mode you will need to press i).  Change the memory_limit to something bigger.  For me I changed it to 2G.

memory_limit = 2G

Save and close vim, press [Esc] key and type :wq!

[esc]:wq!

Done.

Always a good idea to run through the above script again and test the memory_limit value has been updated and now reflects your new value rather than 128MB.

How come I changed the memory_limit from 128M to 2G?

At face value, yes this seems quite a significant jump.  A clue indicating that you need to have a decent jump is in the error itself.  Actually there are two areas of css:

  1. Allowed memory size of 1610612736 bytes exhausted
  2. Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors
Allowed memory size 1610612736

Converting 1610612736 bytes to GB is 1.61G.  Therefore the current allocation of 128M is severely inadequate.

Check memory-limit-errors message

In the last line of the error message there is a reference to a URL on the getcomposer.org site.  If you go to this site you will read

Composer may sometimes fail on some commands with this message:

PHP Fatal error: Allowed memory size of XXXXXX bytes exhausted <...>

In this case, the PHP memory_limit should be increased.  Note: Composer internally increases the memory_limit to 1.5G.

The site also outlines setting an unlimited amount, which can be achieved by entering -1.  Personally, I prefer to set a finite allocation and test the allocation.  If you want to monitor how hungry Composer is you can open a second window and log in to the server.  This while the composer command is running, in the second window run the command

free -m

This will output how the current memory resources are being allocated.

Error — E212: Can’t open file for writing

On a recent change to the memory_limit, I had an E212: Can’t open file for writing error.  This was a quick resolve as I had logged in to the server as a user other than root.  The memory_limit change required me to be a root user.  Once I had another connection through shell as a root user the update was seamless.

Quick snapshot of your memory size by the error

If you aren’t sure what your PHP memory limit is set to, it’s helpfully included in the error message. The size is reported in bytes:

PHP: Fatal Error: Allowed Memory Size of 8388608 Bytes Exhausted — 8 MB
PHP: Fatal Error: Allowed Memory Size of 16777216 Bytes Exhausted — 16 MB
PHP: Fatal Error: Allowed Memory Size of 33554432 Bytes Exhausted — 32 MB
PHP: Fatal Error: Allowed Memory Size of 67108864 Bytes Exhausted — 64 MB
PHP: Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted — 128 MB
PHP: Fatal Error: Allowed Memory Size of 268435456 Bytes Exhausted — 256 MB
PHP: Fatal Error: Allowed Memory Size of 536870912 Bytes Exhausted — 512 MB
PHP: Fatal Error: Allowed Memory Size of 1073741824 Bytes Exhausted — 1 GB
 

Понравилась статья? Поделить с друзьями:
  • Fatal error 190 too many error messages on one line
  • Ffxiv error code i2501
  • Fatal error 183 cannot read from file steamworks
  • Ffmpeg open input error number 138
  • Fatal error 183 cannot read from file shop