How to Reduce PHP and WordPress Memory Usage
When a server runs out of memory, the server must kill processes so the server can keep running. This is the most common cause of MySQL crashing: the server ran out of memory and had to kill MySQL. The best solution to running out of memory is to upgrade to a server with more memory.
What Uses Memory
The memory your server uses is related to many factors, including
- the number of apps on the server,
- what types of apps they are (WordPress, Magento, Drupal, Laravel, etc.),
- any app plugins, such as WordPress plugins,
- the amount of concurrent traffic individual apps receive,
- the PHP version you're using,
- whether you've added third-party PHP extensions, and
- whether you're using PHP's opcache.
Average Memory Usage
The operating system and core services use around 150MB of memory. Each simple app (small codebase, one database, low traffic) uses about 50MB of memory. So, for example, with 10 apps your server would normally use about 650MB of memory. That is, 150MB (OS memory usage) + 10 (number apps) * 50MB (app memory usage) = 650MB
Decreasing Memory Usage
If you need to lower memory usage, you can do a few things to save memory.
Upgrade Each App's PHP Version
Each new version of PHP includes optimizations that reduce that amount of memory used internally by PHP to execute your scripts. PHP 7.1 uses less memory than PHP 7.0, which uses less memory than PHP 5.6, which uses less memory than PHP 5.5, which uses less memory than PHP 5.4. You should use the newest version of PHP that your app supports.
Signup for our newsletter and download a stunning Wordpress admin theme for free!
Disable WordPress Plugins
Disable any WordPress plugins that you don't need. Some WordPress plugins increase memory usage significantly. By disabling plugins you don't need, you can save memory.
Disable Third-Party PHP Extensions
f you've added third-party PHP extensions like New Relic, these will increase memory usage.
If you need these extensions, you should not disable them. However, if you've added them and aren't using them anymore, you should disable them.
Disable the PHP Opcache
PHP 5.5, 5.6, 7.0, 7.1, 7.2, 7.3 and 7.4 have a built-in opcache that is enabled by default.
The entire purpose of PHP's opcache is to speed up your scripts and save CPU by storing compiled PHP scripts in memory.
If you'd rather to have slower apps and increased CPU usage to save memory, you can disable the opcache.
To disable PHP's opcache, you can SSH in as root and edit the following files:
AddOutputFilterByType /etc/php5.5-sp/conf.d/opcache.ini /etc/php5.6-sp/conf.d/opcache.ini /etc/php7.0-sp/conf.d/opcache.ini /etc/php7.1-sp/conf.d/opcache.ini /etc/php7.2-sp/conf.d/opcache.ini /etc/php7.3-sp/conf.d/opcache.ini /etc/php7.4-sp/conf.d/opcache.ini
add the following line to the end of each of these files:
AddOutputFilterByType /etcsudo service php5.5-fpm-sp restart sudo service php5.6-fpm-sp restart sudo service php7.0-fpm-sp restart sudo service php7.1-fpm-sp restart sudo service php7.2-fpm-sp restart sudo service php7.3-fpm-sp restart sudo service php7.3-fpm-sp restart
By far most Wordpress sites are hosted on shared hosting servers. This is a chance for your. Hosting your Wordpress site on it´s own vpsmodel cloud server brings much more security and for sure makes your site run a lot faster from the start. Within a minute you could be up and running your Wordpress sites on a fast, secure cloud server. Just give it a try.