PHP Memory Limit Error

By Mahbub Shahin

July 8, 2023

The PHP memory limit error occurs when the amount of memory allocated to PHP scripts or processes is insufficient to execute a particular task. PHP, being a server-side scripting language, requires memory to handle various operations, such as processing large files, executing complex code, or handling heavy database queries. When the memory limit is reached or exceeded, it triggers the PHP memory limit error.

Why Does This Error Happen

The error that gets outputted can look like this:
Fatal error: Allowed memory size of X bytes exhausted (tried to allocate x bytes) in /path/to/script

Or Like This

PHP Fatal error: Out of memory (allocated X) (tried to allocate Y bytes) in /path/to/script

This error can happen due to several reasons. Here are some common causes:

Insufficient PHP memory limit setting:  The default memory limit set in the PHP configuration may be too low to handle resource-intensive tasks.

Large or complex operations:  Processing large files, manipulating large datasets, or executing complex algorithms can consume more memory than the allocated limit.

Memory leaks:  Poorly coded plugins, themes, or custom scripts can result in memory leaks, where memory is not released properly, leading to memory exhaustion.

How To Fix This Error

To fix the PHP memory limit error, you can follow these steps:

Increase the PHP memory limit:  You can modify the memory_limit directive in the php.ini file or use the ini_set() function in your code to increase the memory limit. However, this may require access to server settings or assistance from your hosting provider.

WordPress configuration file (wp-config.php)

Open the wp-config.php file located in your WordPress root directory and just after the line: That’s all, stop editing! Happy blogging. Add: define( ‘WP_MEMORY_LIMIT’, ‘256M’ );

PHP configuration file (php.ini)

This is usually located in the root hosting directory or in your WordPress install directory. In some cases, the file will need to be generated from your hosting’s cPanel or created manually in the WordPress install directory.  Once the php.ini file has been created/generated open it and include the following at the bottom of the file. memory_limit = 256M

Another option is to increase the PHP limit:  If you’ve configured the server with a low PHP limit, it can cause issues so it’s better to increase the limit.

hPanel: In hPanel, you are able to change the values of a lot of PHP parameters. For that, just go to Websites → Manage, search for PHP Configuration on the sidebar, and click on it:

divigrid hpanel confiq

Then, select the PHP Options tab. There, below the basic options, you are able to define the values of several parameters, like timezone, disabled functions, and limitations: execution time, file uploads, input time, memory limit, and others.

divigrid hpanel php limit

To set a new value for limits, just insert the desired value or select it from the drop-down list, if available:

cPanel: You can configure the memory_limit option for PHP in cPanel hosting from the dashboard. Simply go to  Software toggle section-> Select php version->options
divigrid How to Fix Divi Builder Timeout Error Google Docs

Optimize your code:  Review your PHP code, plugins, and themes to identify any memory-intensive operations or memory leaks. Optimize your code by using efficient algorithms, releasing unnecessary memory, and avoiding unnecessary resource consumption.

Disable unnecessary plugins and themes:  Deactivate or remove any plugins or themes that are not essential. Some poorly coded or resource-intensive plugins/themes can significantly increase memory usage.

Use caching mechanisms:  Implement caching techniques, such as object caching or page caching, to reduce the server load and minimize the need for repetitive memory-intensive operations.

Upgrade your hosting plan:  If you consistently encounter PHP memory limit errors, consider upgrading your hosting plan to one that offers higher memory allocation or dedicated resources.

Seek assistance from hosting support:  If you are unable to modify the memory limit or optimize your website, reach out to your hosting provider for guidance. They can help adjust the memory limit or provide recommendations to resolve the issue.

The PHP memory limit error can be frustrating and disruptive, causing your website or application to crash. However, by understanding the causes and following the necessary steps to fix the error, you can overcome this issue and ensure the smooth functioning of your PHP scripts.

It’s important to keep in mind that increasing the memory limit alone may not always be the optimal solution. Optimizing your code, identifying memory-intensive operations, and addressing memory leaks are crucial steps in preventing the error from occurring in the first place. Regularly reviewing and optimizing your plugins, themes, and custom code can go a long way in improving memory usage efficiency.

If you’re unable to resolve the PHP memory limit error on your own, don’t hesitate to seek assistance from your hosting provider or a developer with expertise in PHP. They can provide valuable insights, suggest alternative approaches, or offer advanced troubleshooting techniques.

Get 10% OFF for Subscribe Now

0 Comments

Submit a Comment

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

You May Also Like