Skip to main 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.