Linux swap space for cloud servers like AWS EC2; DigitalOcean or your local machine

Hi everyone,

We have decided in my company to make all the dev team make on the same distribution, we’ve choosen for that, DigitalOcean.
DigitalOcean doesn’t actually offer multilevel account managements, so each of us had to create a specific Droplet, we have then made database replication to centralize our work, we will do further later.

When I tried to update composer on my symfony project, I’ve got this error:

Fatal error: Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar:///var/www/headoo/composer.phar/vendor/symfony/console/Symfony/Component/Console/Application.php:974

What does it mean?
It means that our OS is using a lot of memory and that there is not enough memory available to do some actions. Why?
By default, Linux tends to use all the RAM to speed up its operations, particularly caching datas.
Therefore it can easily happen in situations where the RAM is used at 90% or more, while treatments only need the half.

Let’s explain more
Simply run “free -m” command to see how many memory is available on your machine, and let’s analyze the result.
Here, we will work on a 7.5GO ram available machine.

[root@~]# free -m
             total       used       free     shared    buffers     cached
Mem:          7873       7289        583          0        251       4272
-/+ buffers/cache:       2765       5107
Swap:         4095         70       4025

It seems to be more than 7 GB of RAM used, more than 4 GB for the cache (buffers and cache), see the last column.

Ok right, so, how to solve this problem?

1. First, you can empty linux cache

[root@~]# sync; echo 3 > /proc/sys/vm/drop_caches

2. Swap some space. What is a swap?
The swap is a “virtual” physical memory often installed on a separate Linux partition. Part of hard drive is reserved for this virtual memory that could relieve the system in case of overload.

3. How to swap some space?
Type in your console:

dd if=/dev/zero of=/swapfile bs=1M count=1024 #count must be equal to the desired block size you need
mkswap /swapfile                              #Setup the swap file with the command
swapon /swapfile                              #Enable the swap file immediately but not automatically at boot time
/swapfile swap swap defaults 0 0              #Enable swap file at the boot time

Right! Now your problem must be solved, if not, do not hesitate to contact me please.

Linux swap space for cloud servers like AWS EC2; DigitalOcean or your local machine