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.