Swap “your” Space
- Jul 13, 2022
- 3 min read
Let's dive a little deeper this time.
We all know that every PC runs on Operating System and every OS has a specified amount of RAM allocated for processing. For data or a program to run on a system, It needs to be loaded into RAM first. The space available in a typical RAM is sometimes not enough for bulk processing. If the memory is too low it might not be able to hold all the data that the CPU needs. What if the memory is too high, then it's costly and sometimes underutilized. We need to allocate the space in such a way that it meets our workloads in an ideal way. Therefore we need to implement a strategy to add more space to RAM without compromising the existing one.
There exists a concept of allocating a part of the memory from the existing hard drive as an addition to the RAM. This memory is solely allocated for RAM and cant be used as permanent storage. The processing will be slow but it can hold smaller workloads as per our needs. In windows, we call it a virtual memory. In Linux, this borrowed space is called Swap Memory or Swap Space.
What is a Swap space or Swap memory?
Swap memory is a dedicated space from a hard drive that can be used by RAM whenever it runs out. The inbuilt memory management program in Linux takes care of this process. This program is capable of finding the inactive blocks of data from the RAM and moving those blocks into the swap space. Ram will be freed and can be used for active data blocks.
Types of Swap Memory
We can allocate Swap memory as a
1. Swap partition:
A partitioned space from the dedicated hard drive is allocated for swap memory.
Or
2. Swap file:
A swap file can be created if or when there isn't sufficient space for a partition in the hard drive.
A typical Swap should equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB.
if (Amount of RAM(GB) < 2) Swap = RAM *2 else Swap = RAM + 2
Swapping in EC2
Q. I want to create a swap file in EC2. How can I do it?
Step 1: We can use the dd command for creating a Swap file.
* The command dd is primarily used for converting and copying files. It can also be used to backup,(create and restore image) of the hard disk.
$ sudo dd if=/dev/zero of=/swapfile bs=128M count=32bs(Block Size) should be less than your available memory.
You created a swap file of 4GB(128*32)
Step 2: Update Read-Write permission using chmod
$ sudo chmod 600 /swapfile*chmod command is used to change the access mode of a file.
Step 3: Create a Linux Swap area
$ sudo mkswap /swapfileStep 4: Now Let's make the swap file available for use
$ sudo swapon /swapfileStep 5 : (Optional) You can verify if the procedure was successful by
$ sudo swapon -sOr
$ sudo free -hStep 6 : If you want to start the Swap file in boot time you need to add a line in the fstab (a configuration file)
$ sudo vi /etc/fstab*vi is a file editor
Step 7: Add this at the end of the line in fstab
/swapfile swap swap defaults 0 0Q. What if I need to remove the Swap File?
Step 1 : If you decide to remove or deactivate the Swap File
$ sudo swapoff -v /swapfileStep 2: You have to remove the line from fstab file.
swapfile swap swap defaults 0 0Step 3: Delete the file using rm command
$ sudo rm /swapfileLet's make Swappiness
Swappiness is a Linux Kernel property that describes how often the system will use the swap space. The value can be between 0 to 100.
Lower value: not very often.
Higher Value: often.
The default value for swappiness is 60.
You can get the swappiness value by
$ cat /proc/sys/vm/swappinessSuppose you want to set a swappiness value.
Step 1 : Set the value for swappiness.
$ sudo sysctl vm.swappiness=(YOUR VALUE)Step 2: To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:
vm.swappiness=10Note: Always remember that the optimal swappiness value depends on your system workload and how the memory is being used. Don’t aim for a higher value at the beginning. Try incrementing from a lower value till you reach an optimum one.
Last but not least:
As I always say, this is just the tip of the iceberg. You can do wonders if you dive deep into this concept.
Don't forget to be Swappy…



Comments