Creating Swap file on Linux Server

Recently, we had issues with java error on our server (java.io.IOException: error=12, Cannot allocate memory). Google said its because of insufficient swap space, but when i checked the server, it had enough swap space. May be that was not enough for the software to work. So, I added a swap file of 1 gigs to check if it works and that helped.

Here is how you enable swap file on linux server:

For such “one shot” needs, we can add temporarily a swapfile that we can remove once the job is done.

To do this, these are the steps:

1. We have to create the swapfile by using the command:

  • dd if=/dev/zero of=/swapfile bs=1024 count=1024000

This example creates a 1GB swapfile (the “count” parameter is calculated multiplying the block size for the size in MegaBytes of the file we want to create)
2. We have to make the created file a swapfile with:

  • mkswap /swapfile

3. We have to activate it with:

  • swapon /swapfile

We can check the active swapfiles with: cat /proc/swaps

After the job is done, we can remove the swapfile in this way:

  • swapoff /swapfile
  • rm /swapfile
Advertisements