If you have read about Linux then you must know about Hibernation which is also called as Suspend to Disk, which does use swap. When you need to use a swap partition then there is at least a size of physical memory for this purpose. Because you need to be able to write out the contents of the physical memory to the disk. In this case, it is actually better to use a swap partition instead of a swap file.

Modern laptop batteries have also got quite a bit bigger in fact even if we do want to do a lot of suspending most of the times we can do a suspend to RAM or just asleep. We don’t really need to do a hibernate. So as far as most of the people concerned that hibernation is not really important enough to require us to have to have a swap partition.

Now if you have a used case which demands to have a hibernation then certainly you are going to need to create a swap partition and plan that in your partition table. But in the general case, you probably don’t really need it.

Let’s see what happens if we start doing partitions? You will notice that the system will become painfully slow to use and becomes unusable under a heavy swap load. Now you will also come to know that unusable situations can occur even if you run your system without swap. It might be demonstrated using the following example:

linux swap partition

For that let’s first start your virtual machine. It doesn’t matter if you have a simple Linux system. Run top command and you will come to know about your RAM i.e. how much RAM you have and how much RAM is free. Now let’s try to run this system without swap at the same time try to create a process that uses up all the available memory. You can create such a process by writing a program in C language in a file. The program or process could be anything like a process which uses or leaks the allocated memory.

The whole reason for running this process is to slow down the system and it leaks away from the memory slowly enough so that you can see what happens before the process itself gets killed. Don’t forget to write return() statement in the program so that it will get compiled.

Now before running this program add some swap space to the system because you are leaking away from the memory so eventually you are going to leak more memory that you have. Use the dd command for adding swap file and name output file. In the program, you can allocate any size of memory as per the free memory you have.

Now create a swap region on this file by running mkswap command and make the swap file available to the system. You may get a warning about insecure permissions but that is not the concern for this example.

Now if you again run the top command then you will come to know that your total amount of RAM is going down and you can see that your swap space utilization is going up because the operating system is taking the contents of memory and writing them out to disk.

You may find that your mouse pointer is becoming little sluggish and if you try to open some another program on the system then you will see that the process which was leaking out the memory gets stopped as well as it will take some more time to open the new program. Same way you can see how long it takes to close that new program.

Now you must have come to know that running such a type of processes makes everything locking up and even the top output is jittery. No one is interested in using such a regular desktop system and you would not be able to enjoy this experience. Thus this is a pretty poor outcome.

Thus when you ran out of the swap space what happens is that at the time when that program was leaking out the memory, it even tries to leak away more memory but it is not available , so the process gets killed and once that process was killed you can see that the time to open any other program or closing that program  is  quicker because what is happening now is that the content is getting back to main memory and we have some amount of free memory.

Now turn off the swap with swapoff command. It will take back all the content from swap file and put it back into the main memory. You can see that swap is zero. It doesn’t mean that not having a swap is not a guarantee that you will experience an unusable slowdown.

What happens is that we start to run out of the memory and down below 215 MB of memory, the mouse pointer doesn’t move because there is no memory left to do it.

Finally the operating system kills the process and reclaim all of that memory so having swap can get you a little bit longer before you run out of memory but if you have a process that leaking away memory then you will sooner or later run out of swap as well and if you do start swapping, it would be so slow that you are not going to use the system anyway.

So if you have enough memory you probably don’t need a swap but you can make a swap on it. It is not necessary to add it at the time of partitioning. You can just create a swap file, make the swap on it and run swap on it. You can put it into your file system table. So it gets done automatically at boot time and you have a swap file with no trouble.

 

Visit website – Hostdens | Web Hosting India


Vishwajit Kale

Vishwajit Kale blazed onto the digital marketing scene back in 2015 and is the digital marketing strategist of Hostdens, a company that aims to provide reliable web hosting solutions. Vishwajit is experienced in digital and content marketing along with SEO. He's fond of writing technology blogs, traveling and reading.