<< All Posts
Don't have enough RAM memory on server? Let's create a swapfile!


Hello!

I’m running a simple instance on DigitalOcean with 512MB of RAM. So, what happens is when I try to update composer to install a new dependency on my symfony2 project, the composer got killed. Why? Well.. composer is always starving and his food is RAM memory. Yeah, it’s a ram memory destroyer. So, let’s fix this creating an extra 1GB Swapfile because we have 20GB of storage in our instance!

1) Let’s check how many available space we have in our root partition / (In my case 16GB)

df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        20G  2.8G   16G  15% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            235M  4.0K  235M   1% /dev
tmpfs            50M  360K   49M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            246M     0  246M   0% /run/shm
none            100M     0  100M   0% /run/user

2) Now, let’s create an empty 1GB file on / with the name swapfile

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 4.57631 s, 235 MB/s

3) If everything ran well, we have 1GB less on our / partition. Let’s verify?

df -h 
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        20G  3.8G   15G  21% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            235M  4.0K  235M   1% /dev
tmpfs            50M  360K   49M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            246M     0  246M   0% /run/shm
none            100M     0  100M   0% /run/user

3) Yeah! Now, let’s change from a single and normal file to a swap file! And then add on /etc/fstab to make it automatically started when boot the server.

sudo mkswap /swapfile 
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=a418b9f4-c4fc-4d8f-87ff-d924e33203cf
sudo swapon /swapfile 
echo 'echo "/swapfile  none  swap  defaults  0  0" >> /etc/fstab' | sudo sh

4) We can confirm that we have a new swap with 1024GB:

free -m
             total       used       free     shared    buffers     cached
Mem:           490        483          6         50          2        298
-/+ buffers/cache:        182        307
Swap:         1023          0       1023

Now we can run our php composer.phar update with no “Killed” messages!

See ya!
Rodolfo



<< All Posts