Thursday March 30, 2017
Swap space are useful for systems having less memory (RAM). If your system facing problem of lack of memory continuously and you don’t want to increase memory on server, Then it can be helpful to enable swap in your system. Swap is comparatively much slower than physical memory but operating system uses swap space in case system goes out of memory. To know more about working of swap visit here.
This article will help you to enable swap filesystem in your running instance. There are two methods to add swap in Amazon ec2 linux instances. This article has been tested with CentOS 6.5 only, but it can work on most of Linux distributions
Method 1: By Creating File in Current File System
This option is helpful if we don’t want to add extra disks in our systems, In this we simply create a file in our current file system and make it type swap, which can be used as swap in our system. Use following commands to create and enable swap on our system.
# dd if=/dev/zero of=/var/myswap bs=1M count=2048
# mkswap /var/myswap
# swapon /var/myswap
bs=1M count=2048 means it will create 2GB of swap file, You may change as per you need. After enabling swap we can see that our system has swap enabled by running “free -m” command.
To make it enable on system boot, simply edit /etc/fstab file and add following entry at end of file.
/var/myswap swap swap defaults 0 0
Method 2: By Adding new Disk in System
This option is helpful if you do not have enough space in our current drives mounted in system. In this option, first we need to add extra disk in our system first. In my case new disk mounted as /dev/xvdd (It may change in your case)
# mkswap -f /dev/xvdd
# swapon /dev/xvdd
To make it enable on system boot, simply edit /etc/fstab file and add following entry at end of file.
/dev/xvdd swap swap defaults 0 0
Hope this will help you!
Please Remember me in your prayers!
Enjoy
