CentOS 7 cannot allocate memory during a “yum install” operation

You can create swap file:

fallocate -l 512M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

This will create swap file and you will be able to install applications. To remove swap file:

swapoff -a
rm -f /swapfile

see Arch wiki for details.


This issue is the server seem to only have 490MB of RAM on the machine and you have 421MB used. Leaving only 68MB free; that is not a lot of memory for any system to work with.

Looking at the output from top shows that MySQL (aka: mysqld) is the culprit.

The best thing you can do if this is a temporary situation is to simply stop MySQL like this:

sudo service mysqld stop

Then with MySQL stopped, you can run yum install as expected.

But another solution would be to use a script like “MySQL Tuning Primer” to help assess the MySQL usage and configuration on the serve and adjust accordingly. The reason being is plain vanilla MySQL out of the box will be a memory hog. But “MySQL Tuning Primer” will help assess your install and let you know what you can tweak. Including lowering memory requirements so the setup can be happy with your limited resources. The only catch is MySQL needs to be actively running for at least 48 hours straight for the “MySQL Tuning Primer” results to be worth anything. Past that, performance tuning MySQL with this script a great way to fine-tune your L.A.M.P. setup.

Additionally, since you are running Apache you can probably lower the RAM requirements for Apache (aka httpd) so you free up more RAM that way. This is a fairly generic set of tweaks for a basic development environment of Apache, but should help you. First open up your Apache config via your favorite command line editor like this; I prefer nano but any text editor is good:

sudo nano /etc/httpd/conf/httpd.conf 

Now find the line that a says Timeout and change that to “120”; two minutes is a reasonable timeout window:

Timeout 120

Similarly find MaxKeepAliveRequests and change that to “24”; “keep alive” connections are good, but don’t let them overwhelm your setup:

MaxKeepAliveRequests 24

And find KeepAliveTimeout and set that to “2”; this should correlate to how fast one page on your site loads and 2 seconds is a good average:

KeepAliveTimeout 2

Now look for the XML config directive set as <IfModule mpm_prefork_module>:

<IfModule mpm_prefork_module>
  StartServers           8
  MinSpareServers       16
  MaxSpareServers       32
  ServerLimit           40
  MaxClients            40
  MaxRequestsPerChild 2000
</IfModule>

The key to this is ServerLimit and MaxClients. By default Apache’s settings are quite high; 255 for MaxClients I believe. But the reality is even a high traffic site will only get 70-80 connections per second… And then die… Meaning Apache connections are stateless so the benchmark is connections per second. So for a development or small scale server, “40” is a good number.

Now with those key adjustments made, restart Apache like this:

sudo service httpd restart

By adjusting MySQL and Apache to have more reasonable settings than default/canned values can free up resources on your server and make everything run more smoothly.

Tags:

Yum

Centos 7