Fatal error: Allowed memory size of X bytes exhausted (tried to allocate X bytes)

Fatal error: Allowed memory size of X bytes exhausted errors are one of the most common PHP errors any sysadmin will see while handling technical support requests from end users.

What causes Allowed memory size of X bytes exhausted error?

This happens specially when apps process large amounts of data, this happens most of the time because the web application is trying to load more data than the size defined in the PHP server memory limit for a single process.

The error message is most of the time the same:

Fatal error: Allowed memory size of X bytes exhausted (tried to allocate X bytes)

Altought the bytes reported in the error can change because of your own memory limit, in the next table you will find the most common sizes in bytes, but converted into MB for better understanding.

Contents

Table of Allowed Memory Size Errors in MB

[table “” not found /]

How can I increase my memory limits on Linux?

There are many ways to increase PHP memory limits, in this post we include the most common scenarios, for CentOS plain servers, and also for cPanel, using the most common software apps like Wordress, Joomla and Drupal, and also generic techniques to increase the size of php’s memory_limit using .htaccess, local php.ini and general server php.ini files.

Incease memory_limit at local php.ini inside public_html directory

If your server configuration allows you to use php.ini insde your public_html directory, then you can place this configuration inside a php.ini file:

memory_limit = 256M

Increase memory_limit using .htaccess

Another way is using .htaccess files, you can edit or upload a .htacces file and place the next configuration inside your public_html directory:

php_value memory_limit 64M

Modify php memory_limit at your general server php.ini file

If you need to set a higher limit for memory_limit variable for your server (this will affect all websites), you can do it by editing your php.ini main file, usually located at /etc/phpl.ini on CentOS and /usr/local/lib/php.ini on cPanel systems.

[[email protected]:~]grep memory_limit /usr/local/lib/php.ini
memory_limit = 256M ; Maximum amount of memory a script may consume (32MB)

Altought you can edit the cPanel php.ini file, it is recommended to make this change directly using WHM interface, using:

Home » Service Configuration » PHP Configuration Editor

Switch to ‘Advanced Mode’, search for memory_limit variable, and set it to match your needs.

Ensure the changes are applied by restarting httpd

service httpd restart

Verify your PHP limits with php.info

To verify that the new setting is active, create a PHP test file that contains the following code in the same directory where the .htaccess file is located:

<?php phpinfo(); ?>

Increasing the PHP memory limit at the application level

If you need to update the value of your memory limit by tweaking your PHP code, you can do it by adding this code to your php main file.

ini_set('memory_limit','256M');

Increasing memory_limit on WordPress

If you are using WordPress, then you can try setting this WP_MEMORY_LIMIT variable inside config.php file. Edit your wp-config.php file and enter something like:

define('WP_MEMORY_LIMIT', '256M');

Increasing php memory on Drupal installations

For Drupal installations, the file changes and it’s called ‘settings.php’.
This file is located at this location sites/default/settings.php

Locate the PHP settings section and add the following line at the end of that section:

ini_set('memory_limit','128M');

Increase php memory_limit on Joomla

If you are using Joomla, you can increase its memory limit adding the following line inside your index.ph file.

ini_set('memory_limit', '384M');

Conclusion

There are many ways to increase php memory limits, on this post we tried to cover most of the common fixes for this Fatal error: Allowed memory size of X bytes exhausted. If you know other ways to fix this PHP memory error, please share your knowledge with us using the comment form.

Further reading:

PHP documentation about PHP memory limits: http://php.net/manual/en/ini.core.php#ini.memory-limit

About the Author: Martin Keler

Leave a Reply

Your email address will not be published. Required fields are marked *