Tuesday, 8 November 2005

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 my_swap_file) temporarily.
  1. First create a file of size 64 MB.
    # dd  if=/dev/zero  of=/my_swap_file  bs=1024  
    count
    =65536
    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
  2. Make the permissions read and write for owner and no permissions for group and others.
    # chmod 600 /my_swap_file
  3. Prepare the file to be used for swapping.
    # mkswap my_swap_file
  4. Start using the swap file
    # swapon my_swap_file
If you want this file to be used for swapping across reboots (permanently), then enter the following line in the /etc/fstab file.
/my_swap_file   swap   swap   defaults   0   0

No comments:

Post a Comment