Fixing the PHP Error: Allowed memory size of X bytes exhausted
Evans
Last Update pre 2 meseca
The error message:
…indicates that your PHP script has tried to use more memory than the server's PHP configuration allows.
This typically occurs on resource-intensive sites (e.g., WordPress, Magento, large uploads, or image processing) and may affect specific scripts or even cause a white screen of death (WSOD).
1. Increase PHP Memory Limit via .htaccess (for Apache-based servers)
If your site uses Apache (common with cPanel and DirectAdmin), you can increase the PHP memory limit by editing the .htaccess file.
Open or create a .htaccess file in the root of your website (/public_html or /domains/example.com/public_html/)
Add the following line:
- Save the file and reload your site.
Some servers restrict using php_value in .htaccess (especially on PHP-FPM setups). In such cases, use php.ini or .user.ini.
➤ For php.ini (if supported by your hosting):Create or edit a php.ini file in your website root.
Add:
Create or edit .user.ini in your website root.
Add:
Save and wait a few minutes for the changes to apply (or restart PHP if you have control over the server).
In cPanel:
Go to Select PHP Version > Options (or "MultiPHP INI Editor").
Locate memory_limit and increase it (e.g., 256M or higher).
Save your changes.
Navigate to User Level > Select PHP Version or PHP Settings.
Find memory_limit, adjust it, and save.
If the issue persists even after increasing memory, the root cause might be:
A faulty plugin/module (especially on WordPress, Joomla, or Magento)
A script entering an infinite loop or handling too much data
Recursive function calls with no exit condition
You can:
Enable PHP error reporting/logging to see the specific file and line
Disable non-essential plugins and test again
Review /logs/error_log in cPanel/DirectAdmin or /var/log/php-fpm.log on custom servers
If none of the methods above solve the problem:
Check your hosting plan’s maximum memory limit (some shared plans cap it)
Open a support ticket
Consider upgrading to a plan with more resources (e.g., VPS or dedicated hosting)
Summary
Method | Location | Example code |
.htaccess | /public_html/ or subfolder | php_value memory_limit 256M |
php.ini | Website root | memory_limit = 256M |
.user.ini | Website root | memory_limit = 256M |
Panel GUI | cPanel / DirectAdmin | Adjust via PHP Settings |