Pages

Subscribe:

Sunday, November 23, 2014

Understanding, Creating, Extending, Calculating the Swap Space in Linux

1.       Concept of swap space in Linux?
Ans:- Linux allows a hard disk to be used as memory (virtual memory) apart from the RAM( physical memory) so that the kernel is able to write off the unused content from the RAM to this virtual memory and whenever the content is required again it writes back. 


Since the unused content no longer holds the space in physical memory it can be used by some other process. All this is done internally and a user is never aware of the backend process.

2.       Creating a Swap Space in Linux?

Ans:-    $ dd if=/dev/zero of=/extra-swap bs=1024 count=1024
        1024+0 records in
        1024+0 records out
        $

dd if is used to create a file
of is used to create the file name that is extra-swap here
bs is for the size in MB
count  defines the size of this swap space , it is good to keep the size in multiple of 4 since kernel  writes out memory pages  4kb in size , and if its not a multiple of 4 than last few bytes might not be used.


$ mkswap /extra-swap 1024
        Setting up swapspace, size = 1044480 
        bytes
        $

mkswap is used to make the swap partition. It is good to keep the swap partition as file system 82 that is swap space so that swap space can be easily identified later. But this is not necessary for the kernel to recognize the swap space.

NOTE:- You should be very careful when using mkswap, since it does not check that the file or partition isn't used for anything else. You can easily overwrite important files and partitions with mkswap! 

swapon /extra-swap
$
Swap spaces can be used automatically by listing them in the /etc/fstab file.
/dev/hda8        none        swap        sw     0     0
/swapfile        none        swap        sw     0     0
The startup scripts will run the command swapon -a, which will start swapping on all the swap spaces listed in /etc/fstab. Therefore, the swapon command is usually used only when extra swap is needed.

You can monitor the use of swap spaces with free. It will tell the total amount of swap space used.
free
             total       used       free     shared  
 buffers
Mem:         15152      14896        256      12404       2528
-/+ buffers:            12368       2784
Swap:        32452       6684      25768
$


The first line of output (Mem:) shows the physical memory.
That last line (Swap:) shows similar information for the swap spaces. If this line is all zeroes, your swap space is not activated.
The same information is available via top, or using the proc filesystem in file /proc/meminfo.
All the swap spaces that are used automatically with swapon -a can be removed from use with swapoff -a; it looks at the file /etc/fstab to find what to remove. But Any manually used swap spaces will remain in use.

Swap is not an replacement for the physical memory but only an extension, since it is very slow (1000 times) slower than physical memory.


3.     Calculating the size of the Swap Space?
It is important to understand the amount of swap space required, because allocating too much swap space would not make your system run faster, and too little can obviously creating performance issue, also sometimes it might be asked you in interview also, since the proper answer is a tricky one.
Some people will tell you that you should allocate twice as much swap space as you have physical memory, but this is a bogus rule.
Briefly you should calculate swap as the largest amount of memory you would be needing at a time, this can be achieved by running all the applications at a same + security + some additional ( in case something is left) – Physical memory = swap space. Details about this computation is coming in my next post, but if in case your computation comes out to be too large , than you should consider adding more of physical memory than swap for better performance.

4.     How much swap space can be used in single system?

The maximum size of the swap space can be 2GB in file size and maximum number of the swap file can be 8 swap files for a system so that the total count of the swap space for a system is 8 x 2GB = 16GB (Max).

5.     Monitoring of Swap Memory than Physical Memory?

It is always good to monitor the Swap Usage and configure alerts based on the swap usage rather than the physical Memory (RAM) in Linux. Linux has a tendency of using Memory if you are checking through "free" command. It keeps the memory in buffer so that it can make the best usage of the available memory. To make if the memory usage is critical always monitor the swap usage as swap memory usage indicates physical memory has lapsed and system is using the virtual memory and situation needs to be recitied.

0 comments:

Post a Comment