How to Fix “PHP Fatal error: Allowed Memory Size of xxx Bytes exhausted…” on WordPress

PHP has a default memory limit of 8MB which is set in php.ini file. If your .php script exceeds more than the limit it will throw “PHP Fatal Error Allowed Memory Size Exhausted”.

To settle this problem, you can add one line code in the beginning of your script:

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

or, add in the following code in wp-settings.php file on WordPress:

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

WP_MEMORY_LIMIT option allows you to specify the maximum amount of memory that can be consumed by PHP.

or, add in this line in .htaccess file inside your script’s folder:

php_value memory_limit 16M

or, if you want to permanently affect all PHP script, modify memory_limit line in php.ini file:

memory_limit = 16M

Try to increase the memory limit until your script run properly.

This is only a workaround. Your PHP script should not be exceeding 8 MB, unless you are uploading files or doing something else that’s obviously taking up a lot of memory usage. What you should really be doing is trying to figure out why your script is using so much memory and attempt to fix it.

To figure out how much memory your PHP script is using is to use the memory_get_usage() PHP function. Simply echo it at any point in your script to find out where your memory usage is spiking:

echo memory_get_usage();

 

 

Leave a Reply

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