May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Why we give swap partition in double the size of the RAM?

Why we give swap partition in double the size of the RAM?

It has a meaning and for that we should know memory hierarchy. We have different levels of memory which is useful for processing your data. They are as follows.
Processor/CPU registers(Bits in size)
L1 Cache(kbs in size)
L2 Cache(MBs in size)
L3 cache(100s of MBs in size)
RAM(GB’s in size)
suppose take one application which is in running state. The application data is moved to RAM for faster accessing and some of its data is moved to L3 cache for processing frequently used data and we move more frequently used app data to L2 and then L1. And we move data which is right now processed by your processor/CPU to registers which is present in processor. If you observe the memory size will gradually decrease when you go up in the ladder more frequently used data is kept at higher levels and made it self available for processing. So if suppose you want to load a bigger program which can consume all the ram/L3/L2/L1 cache its better to move already running process to some location which we can access later to process it once again. But moving this data to HDD is not preferred as it is bit slow and other solution is to increase the RAM size which bit cost. So people came with a solution called SWAP partition which can solve both these limitations.
We create swap partition to cache all the data in registers, L1, L2, L3 caches and RAM.
Then why we create two times the RAM?
The reason behind it is that if you club all the above memory locations, the total size will be between 1.5 times to 2 times the RAM. This is the reason behind the thumb rule to create swap size 2 times the RAM
Some FAQ related SWAP:

If I create swap size more than two times the RAM, What will happen?
As we said earlier swap is used to move data from different memories and their total size is always 1.5 times to 2 times maximum so it’s of no use if you create swap size more than 2 times the RAM. In other words, all the space more than 2 times the RAM in swap is no use.
I have RAM size of 128GB and my RAM usage never fills up, do I have to create swap partition?

No, not at all required. As mention earlier swap is a temporary space to store all your memories data for future processing, if your RAM is free then there is no use of swap partition.
I have no free partitions left to create swap partition but want to create some swap space is it possible?

Yes, its possible use virtual file system to do it. To know more about this please have a look below,
Linux advanced Swap Management: Swap creation with Virtual file system Advanced Swap management with virtual file system
Swap partition is a place your system will keep temporary files so that it can do the processing bit faster. Suppose your swap is getting filled and there is no raw partition left for creating swap partition on the server what we can do ?. At any cost your manager asked to increase the swap to improve the system performance.
To come out of this situation there is one solution provided by Linux e.i we can create a swap file with in all ready existing and using partition if that partition is having sufficient free space
Step1:Switch off all the swap before any swap related work
#swapoff -a
Step2: Determine what is the swap size we required(here i am taking 128MB) and execut the following command with count equal to 131072 (because 131072 is equal to 128M) This will create a swap file name swapfile in / directory with size of 128 MB.
#dd if=/dev/zero of=/swapfile bs=1024 count=131072
Let me explain above command.
dd is an excellent command which is used to take backups of partitions, create MBR, create empty files etc. Here dd is used to create a empty file called /swapfile by taking input file as /dev/zero with block size as 1024 byts and number of these bytes as 131072. This command will create a file with 128MB.
Step3: Now set this swap file in order to use this virtual file as swap
#mkswap /swapfile
Step4: Edit the /etc/fstab file to specify the swap file for saving these settings permanently.
#vi /etc/fstab
/swapfile swap defaults 0 0
save and exit the file. Learn about vi editor here
Step5: Update the kernel about the mount table changes.
#mount -a
Step6:After all these changes we have to on the swap once again with command as given below.
#swapon -a
Step7: Check weather the swap space is updated or not by using any one of the following command
#free -m
#swapon -s
#cat /proc/swaps
Ok we are done with creating the swap file, how about removing it?. Follow below procedure to remove the swap file.
Removing swap

Step1: Before doing any thing with swap first we have to switch off the swap
#swapoff -v /swapfile
Step2: Remove or comment the line of swap file in /etc/fstab file system configuration file
#vi /etc/fstab
/swapfile swap defaults 0 0
then save and exit
Step3: update the kernel about mount table changes
#mount -a
Step4:Remove the swapfile permanently

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>