How to solve Fatal Error: Allowed Memory Size of XXXX Bytes Exhausted PHP

How to solve Fatal Error Allowed Memory Size of XXXX Bytes Exhausted PHP

In this article let’s understand few ways how to solve Fatal Error: Allowed Memory Size of XXXX Bytes Exhausted in PHP.

The “Allowed Memory Size Exhausted” error occurs when your PHP script exceeds the memory limit set in your PHP configuration. To solve this error, you can try the following solutions:

1) Increase Memory Limit in PHP.ini

  • Locate the php.ini file used by your PHP installation. The location can vary depending on your server setup. You can use phpinfo() function to locate the file.
  • Look for the line that starts with memory_limit and increase the value. For example, you can set it to memory_limit = 256M to allocate 256 megabytes of memory. Set as you need.
  • Save the changes to the php.ini file and restart your web server.

2) Increase Memory Limit in .htaccess

  • If you don’t have access to the php.ini file, you can try increasing the memory limit through an .htaccess file if your server configuration allows it.
  • Add the following line to your .htaccess file: php_value memory_limit 256M
php_value memory_limit 256M
  • Save the changes and check if the error is resolved.

3) Increase Memory Limit in PHP Script:

  • You can also try increasing the memory limit directly in your PHP script using the ini_set() function.
  • Add the following line at the beginning of your PHP script:
ini_set('memory_limit', '256M');
  • This will temporarily increase the memory limit for that specific script execution.

4) Optimize Your Code:

  • Analyse your code to identify any memory-intensive operations or areas that might be causing the memory exhaustion.
  • Check for any unnecessary large data structures, excessive loops, or recursive functions that could be optimized or refactored.
  • Make sure to release any resources or variables that are no longer needed.

5) Use Memory Efficiently:

  • In case your script deals with large datasets or files, consider processing them in smaller chunks instead of loading everything into memory at once.
  • Use proper memory management techniques, such as unsetting variables or freeing up resources when they are no longer required.

Note that increasing the memory limit is not always the best solution. It’s recommended to optimize your code and use memory efficiently to avoid potential memory-related issues in the long run.

That’s all on how to solve Fatal Error: Allowed Memory Size of XXXX Bytes Exhausted PHP.