16

Try to yum install php56w-mcrypt and system gave me this error

libmcrypt-2.5.8-13.el7.x86_64: [Errno 5] [Errno 12] Cannot allocate memory
php56w-mcrypt-5.6.5-1.w7.x86_64: [Errno 5] [Errno 12] Cannot allocate memory

I ran a check on free -m

             total       used       free     shared    buffers     cached
Mem:           490        421         68          8          3         42
-/+ buffers/cache:        376        113
Swap:            0          0          0

top

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 9756 mysql     20   0  727588  81556      0 S  0.3 16.2   1:04.61 mysqld
11089 root      20   0       0      0      0 S  0.3  0.0   0:16.25 kworker/0:2
    1 root      20   0  129396   2020    576 S  0.0  0.4   0:05.88 systemd
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.16 ksoftirqd/0
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
    9 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/0
   10 root      20   0       0      0      0 S  0.0  0.0   0:01.82 rcu_sched
   11 root      20   0       0      0      0 R  0.0  0.0   0:02.10 rcuos/0
   12 root      rt   0       0      0      0 S  0.0  0.0   0:00.91 watchdog/0
   13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 khelper
   14 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
   15 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns
   16 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback
   17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd

Not sure where else to check.

I am using DigitalOcean VPS with RAM 512MB, the $5/month plan.

Giacomo1968
  • 58,727
Muhaimin
  • 271

4 Answers4

24

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.

Giacomo1968
  • 58,727
pbogut
  • 386
14

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.

Giacomo1968
  • 58,727
0

Had similar issue whilst running a yum update on CentOS7, restarting the server fixed it. I suppose it cleared the memory for patches/new packages to be installed.

0

If your server has been running for a long time, a system reboot will often free up your memory.

reboot

With this being said, this is probably only a temporary fix! You should figure out the actual cause of the memory issues after doing this in order to prevent it from happening again. You can use this solution as a quick temporary fix to get your server running again while you further investigate the memory issues.

The first good place to start would be to check your logs.

sudo grep -i -r 'out of memory' /var/log/