Monday, 23 May 2011

Adding a swap file to your Linux system

There are times when you are running a resource hungry application and your system slows to a crawl. At such times, a quick look at the memory usage of your system tells you that your system has used up all the memory including swap memory. This is a situation where you can create a temporary swap file to aid the system. These are the steps to be followed to create and mount a swap file in Linux.
Lets say, you want to add 64MB swap file (named SwapFile) temporarily.
  1. First create a file of size 64 MB.
    # dd  if=/dev/zero  of=/SwapFile  bs=1024  
                                     count=65536 OR
  2. # dd  if=/dev/zero  of=/SwapFile  bs=1M  
                                     count=64
  3. Here bs is block size in bytes and count is the number of blocks to be used. So the total size of the file will be 1024*65536 = 64 MB; 1M*64= 64MB
  4. Make the permissions read and write for owner and no permissions for group and others.
    # chmod 600 /SwapFile
  5. Prepare the file to be used for swapping.
    # mkswap SwapFile
  6. Start using the swap file
  7. # swapon SwapFile
If you want this file to be used for swapping across reboots (permanently), then enter the following line in the /etc/fstab file.
/SwapFile   swap   swap   defaults   0   0
 
Interview Questions and Answers:
1) why do you need to have a swap file system?
 
Linux systems run better with a swap file, even when you
have plenty of RAM. The technical reason is that it's
sometimes easier to swap out "dirty" data than it is to
write it to peoperly to the disk.

In general, Linux will fill up as much of your memory as it
can, in the hope that those little bits might be needed. You
might as well use it (RAM). You paid for it. If it's all
filled up, then at some point, you're going to have to free
some of it up.

Data may be sitting around in RAM waiting to be written to
disk in an efficient and orderly process. This is called
"dirty" data. Sometimes, just because of the way it works,
it's faster and easier to leave the "dirty data" in memory,
but transfer it to swap, thereby freeing physical RAM.
Ideally, it would be best to just free it from RAM entirely,
but this gives the kernel another option.
 
2) Difference between swap partition and swap file?
 
Swap partition maintained seperate partition.
same swap partition can be used in two OS within single 
machine.
If system crashes their is a chance to recover itor may not 
be corrupt the partition.
Less fragmented.

where as swap file takes very less space. we can increase 
the space very easily, compared to swap partition.
SWap file system fragmented.
If system crashes their is a huge chance to lost the swap 
file system.
 
3)Tell me the steps to remove the swap file?
Firstly disable the swap file by “swapoff” command.
Remove Swap file entry from /etc/fstab file.
Now remove the swap file by “rm” command.
 
4) What can we do with “parted” command or utility?

- View the existing partition table
- Add partitions from free space or additional hard drives
- Change the size of existing partitions
Keywords: Increase Swap Space?
How to Increase the Swap Partition
How to Increase the Swap File
Increase the Swap File Permanent
Increase Linux swap file partition swapfile swappartition

No comments:

Post a Comment