[HowTo] Make Swap File in Ubuntu

One tips to make your Ubuntu or other linux faster is by adding a swap space or swap file. Swap space is the area on a hard disk which is part of the Virtual Memory of your machine, which is a combination of accessible physical memory (RAM) and the swap space.

Swap space temporarily holds memory pages that are inactive. Swap space is used when your system decides that it needs physical memory for active processes and there is insufficient unused physical memory available. If the system happens to need more memory resources or space, inactive pages in physical memory are then moved to the swap space therefore freeing up that physical memory for other uses.

You need swap space or swap file if you have low memory but have enough space in your harddrive. Usually you need swap space is double of your RAM. So if you have 512MB RAM, make your swap file 1GB, and so on.

Her is how to make swap file in Ubuntu:

  1. Open your terminal and login as root
  2. Create a large empty file:
    dd if=/dev/zero of=/mnt/512Mb.swap bs=1024M count=512
  3. Format the swap file:
    mkswap /swapfile
  4. Turn the swap file on:
    swapon /swapfile
  5. The additional swap is now available and can be seen by :
    cat /proc/meminfo
  6. Add it to fstab to make it permanent:
    vim /etc/fstab
  7. add this to the file:
    /swap_file       none            swap    sw              0       0

Comments

  1. Ephevos says:

    bs=1024M is wrong you should use either bs=1024 or bs=1M

  2. bull says:

    You are a very great master linux admin, you use three different files:
    1. /mnt/512Mb.swap
    2. /swapfile
    3. /swap_file
    I do not understand, how this work.

  3. martian says:

    What a load of crap. First you make a 512Gb swapfile, the making of which may well kill your machine, then as another person noted you refer to it 3 different ways.

    Take this harmful crap down, my mate hosed his server trying to follow this

Give me your feedback

This site uses Akismet to reduce spam. Learn how your comment data is processed.