Learn how to solve the issue after updating composer to the latest version with a low memory limit or 32 bits based PHP distributions.


Errors based on the VirtualAlloc and VirtualFree methods during the execution of a script in PHP, is explained by a very simple fact, PHP don't have enough memory available to execute it, that's why the exception is thrown and you will find as well the PHP Fatal error message "Out of memory (allocated xxxxxx) (tried to allocate xxxx bytes)". For example, lately with the latest version of Composer this kind of exception is raised when you try to update packages with the composer update instruction:

Loading composer repositories with package information

Updating dependencies (including require-dev)                                                           

VirtualAlloc() failed: [0x00000008] Not enough memory resources are available to process this command.  

VirtualFree() failed: [0x000001e7] Attempt to access invalid address.                                 

VirtualAlloc() failed: [0x00000008] Not enough memory resources are available to process this command.

VirtualFree() failed: [0x000001e7] Attempt to access invalid address.                                

PHP Fatal error:  Out of memory (allocated 1512046592) (tried to allocate 4096 bytes) in phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/DependencyResolver/Solver.php on line 220

Fatal error: Out of memory (allocated 1512046592) (tried to allocate 4096 bytes) in phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/DependencyResolver/Solver.php on line 220 

With the previous afirmation, you may be thinking how can this be possible if i have more than 8GB RAM on my device? Well, unfortunately, the problem is not always the same so according to your case the solution may obviously vary.

A. Solution: increase PHP max memory limit

The first thing that you need to know is the current limit of memory of your PHP distribution. You can quickly know this from the CLI running the following command:

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

In our case, without modifying the php.ini file of our distribution, this command outputs 128M in the console. This is currently the limit that some PHP script has available to run, with composer that's basically the error, so you will need to increase this limit in the mentioned configuration file of PHP (php.ini). You can know which ini file is being used for the CLI version of PHP with the following command:

php --ini

In our case in Windows, this will output:

Configuration File (php.ini) Path: C:\Windows
Loaded Configuration File:         C:\xampp72\php\php.ini
Scan for additional .ini files in: (none)    
Additional .ini files parsed:      (none)

So, open the php.ini file with your favorite text editor and modify the amount of the memory_limit attribute with a considerable value, for example 512M or 1G:

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

Of course the amount will change according to the performance of your script, so you may want to test it and change it according to your needs. Save the file, restart apache and test your scripts again (if you are working with composer, run the commands until it works). If it doesn't work and you face the same exception, increase the value even more until it works (unless you have a 32 bits installation of PHP, where the amount independently of its availability, its max value will be 2G).

If your values don't work and you have a 64 bits PHP distribution, check the monkeypatch solution that defines an unlimited use of RAM for the PHP scripts (absolutely not recommended on production environment, but helpful in development environments to know what's wrong).

Important warning for Windows XAMPP users or 32 bits PHP distributions

Unfortunately, if you get this exception when running some script or composer when you are using XAMPP and the amount of max memory of 2056M isn't enough and you still see the exceptions, let me tell you that you are in a big problem as you will need either to optimize your script or if possible, to move on from a 32 bits distribution to a 64 bits distribution instead.

This problem is based on the fact that 32 bits flavoured distributions (like the widely known XAMPP environment that only offers the 32 bits architecture) won't allow you to use a bigger amount of memory, even when you have for example 16GB of RAM available on your computer. The 32 bits architecture will limit this with a bigger value in the php.ini to its maximal amount of 1996MB - 2056MB.

Last resource tip for xampp Users

If you are working with composer using the PHP distribution of XAMPP and the instruction that you use e.g composer update or composer install fails, it will mean that the code to process the composer.json of your project is requiring more than 2GB of memory to execute which will fail in xampp. The solution pitifully in this case, is to change of stack like Wampp. This dev stack offers a 64 bits version that shouldn't face this issue after increasing the memory limit.

B. Fast monkeypatch solution

If you tried the mentioned solution for the exception with different values but that wasn't enough, you can try to set an unlimited value for the memory limit:

Warning

You shouldn't do this in production, as PHP will have unlimited memory access, which means that if your scripts have a memory leak, your server may become unusable until its restart. Do this only locally in your development environment to test wheter your scripts work or not.

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

If you are using composer when you see this exception, you may want to run the instruction (composer update) along with PHP and targetting the composer.phar file e.g:

php -d memory_limit=-1 "C:\ProgramData\ComposerSetup\bin\composer.phar" update

C. Download a x64 based PHP release

In case that not any of the mentioned solutions worked, then you will need to use a PHP distribution of 64 bits to update the dependencies of your project using composer. Don't worry, basically you can still work with the current PHP version in your project, however you will use another version in the CLI to install/update the dependencies.

Visit the PHP For Windows: binaries and source release website and download a X64 based version of PHP, whatever you want. In our case we will use PHP 7.2 of 64 bits:

PHP x64 Bits release for Windows Download

Extract the zip content in some directory of your system. In our case, we will store it in the desktop: C:\Users\sdkca\Desktop\php72. Inside the directory you will find a binary distribution of PHP of 64 bits, however it isn't configured yet and it will probably fail if your try to run composer with this PHP binary. As first, copy the php.ini-development file that you will find in the root directory and change the name of the copy to php.ini, so the binary will take this file as the default configuration.

On the new php.ini file (C:\Users\sdkca\Desktop\php72\php.ini) you will need to uncomment the extension_dir directive and enable the gd2 and openssl module, otherwise composer will throw errors during the execution of the commands:

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "ext"

; Uncomment as well the gd2 and openssl extensions to enable them in PHP
extension=gd2
extension=openssl

Now that you have the basic configuration for this PHP version of 64 bits, you can now try to run the composer command using this binary:

"C:\Users\sdkca\Desktop\php72\php.exe" -d memory_limit=-1 "C:/ProgramData/ComposerSetup/bin/composer.phar" update

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors