This article has been ported from my personal blog
Below is how to resolve such specific problem.
Oh, this is not the case if you already have a dedicated(partitioned) swap space, it's a better option in any case but in most cases, we will be playing with servers without a swap patition.
Okay, let's get started.
As a general rule, calculate swap space according to the following:
|Amount of physical RAM||Recommended swap space|
|2 GB or less||2 *
|More than 2 GB, less than 32 GB||4 GB + (
|32 GB or more||1 *
Important Note: Swap sapce should never be less than 32 MB!
sudo dd if=/dev/zero of=/swapfile bs=128M count=YOUR_COUNT
In the above command, bs is block size which should be less than the available memory at that moment.
bs * YOUR_COUNT will be your swap file size, for example,
bs=128M count=32 means your swap file will be 4096 MB in size.
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -s
sudo vim /etc/fstab
Add the following line at the end of the file:
/swapfile swap swap defaults 0 0