If you get a PHP error like “Allowed memory size of xxx bytes exhausted” you have to increase your memory limit.
Increasing the PHP memory limit is a common task for WordPress users, especially when encountering errors or performance issues due to insufficient memory allocation. This article provides a comprehensive guide on how to increase the PHP memory limit in a WordPress environment.
Understanding PHP Memory Limit
PHP, the scripting language that powers WordPress, allocates a specific amount of memory for executing scripts. The memory_limit
directive in the php.ini
configuration file defines this allocation. If a script exceeds this limit, PHP halts execution and generates a fatal error, often manifesting in WordPress as the “Allowed memory size exhausted” error.
Default Memory Limit in WordPress
By default, WordPress sets the PHP memory limit to 40MB for single-site installations and 64MB for multisite installations. However, certain themes, plugins, or large-scale websites may require more memory to function optimally.
Methods to Increase PHP Memory Limit
- Editing the
wp-config.php
File- Access the File: Use an FTP client or your hosting provider’s file manager to locate and open the
wp-config.php
file in the root directory of your WordPress installation. - Add Memory Limit Definition: Insert the following line above the
/* That's all, stop editing! Happy blogging. */
comment:define('WP_MEMORY_LIMIT', '256M');
This command requests WordPress to increase the memory limit to 256MB. You can adjust the value as needed.
- Access the File: Use an FTP client or your hosting provider’s file manager to locate and open the
- Modifying the
.htaccess
File- Access the File: The
.htaccess
file resides in the root directory of your WordPress site. Ensure you back up this file before making changes, as incorrect modifications can cause site issues. - Add Memory Limit Directive: Append the following line to the file:
php_value memory_limit 256M
Note: This method is effective only if your server runs Apache and allows.htaccess
overrides.
- Access the File: The
- Editing the
php.ini
File- Locate the File: The
php.ini
file is the main PHP configuration file. Its location varies depending on your hosting environment. If you cannot find it, consult your hosting provider. - Modify Memory Limit: Open the file and locate the
memory_limit
directive. Update it as follows:memory_limit = 256M
If the directive doesn’t exist, add it to the file. - Save and Restart: After saving the changes, restart your web server to apply the new settings.
- Locate the File: The
- Using a PHP
ini_set
Function- Edit the
wp-config.php
File: Open thewp-config.php
file. - Add the
ini_set
Function: Insert the following line:ini_set('memory_limit', '256M');
This method sets the memory limit at runtime. However, some hosting environments may restrict the use ofini_set
it for security reasons.
- Edit the
Considerations
- Hosting Provider Limitations: Some hosting providers impose strict memory limits that cannot be overridden. If you find that your changes are not taking effect, contact your hosting support for assistance.
- Plugin Conflicts: Certain plugins may have their own memory settings or conflicts. Ensure all plugins are updated, and consider deactivating them temporarily to identify any issues.
- Monitoring Memory Usage: After increasing the memory limit, monitor your site’s performance to ensure stability. Tools like Query Monitor can help track memory usage and identify bottlenecks.
Conclusion
Adjusting the PHP memory limit is a crucial step in maintaining a healthy WordPress site, especially as your site grows in complexity. By following the methods outlined above, you can effectively increase the memory allocation to meet your site’s demands. Always remember to back up your site before making any changes and consult with your hosting provider if you encounter any challenges.