Creating SWAP file in linux
I have a small droplet in digital ocean(where this blog is hosted). Recently I was trying to run an application and it was getting killed immediately without any error message. After some googling it turned out that system was throwing memory out of exception. dmesg showed me
dmesg
Even though the application was small, the framework I used had a minimum memory requirement which was more than I had in the droplet(512 mb).
The Solution was to create SWAP memory. SWAP memory is a holder in hard disk which can be used by OS to store data which exceeds RAMs capacity.
Eventhogh it will add more RAM space for OS to work on, this will be much slower since we are accessing hard disk.
But in our case we know that it will not be an issue since our application has small memory footprint.
How to
sudo swapon -s
This command will show you any existing SWAP file. If there is no entries that means there is no SWAP file enabled.
Now we can create a SWAP file under root(/swap).
sudo dd if=/dev/zero of=/swapfile bs=128m count=10
here we are trying to write zeros with block size of 128 mb for 10 times. Make sure that your block size is smaller than the available RAM in your system.
Set permission. ie Allow only root to read and write
sudo chmod 600 /swapfile
Now make the newly created file as a SWAP file
sudo mkswap /swapfile
Check if its created
sudo swapon -s
You can check with free -m also.