how to add swap memory in ubuntu

how to add swap memory in ubuntu

In every computer we have a physical memory(i.e: RAM). When a user starts a program it is loaded from the disk (where the program resides) to the RAM, because RAM is faster than the disk memory. Some processes intially need more memory to startup but after it will not use the complete memory that is allocated to it. When RAM is completely used then there will be no memory for the new processes. In that case linux kernals will kills the process that is consuming the more memory. In ordered to avoid it we add a swap memory to the system.

Swap Memory:  If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory. It is always recommended that system should have twice the amount of the RAM.

Steps to add swap memory to Ubuntu server:

  1. Check the system for swap memory information
    sudo swapon --show
    It will not show output if swap memory is not enabled.
  2. Check for available disk space in the ubuntu system using the below command
    df -h
    above command will give the output something like below
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           386M  2.1M  383M   1% /run
    /dev/sda1        84G   34G   46G  43% /
    We have total of 46 GB available in the above example output. So, we can add the swap memory based on the RAM size.
  3. Check RAM size in MB with below command
    free -h
    above command will give the output something like below
                  total        used        free      shared  buff/cache   available
    Mem:           3.8G        2.2G        208M        363M        1.3G        992M
    It is alway recommended that we should have the swap memory as twice of the RAM. So, For above output we can use 8 GB of Swap memory.
  4. Now, lets create a swap memory using "fallocate", "swapon" commands
    In the above example we have 4GB of RAM. So, We are creating the swap memory of 8GBabove command will give the output something like below
    sudo fallocate -l 8G /swap_mem_file
    sudo chmod 600 /swap_mem_file
    # check the correctness with below command
    ls -lh /swap_mem_file
    # Output: -rw------- 1 root root 8.0G Aug 25 11:14 /swap_mem_file
    # mark the file as swap space
    sudo mkswap /swap_mem_file
    sudo swapon /swap_mem_file
  5. Now, verify the availability if swap memory using below command
    sudo swapon --show
    It will show output something like 
    /dev/sda1 partition 8G   0B   -2
    We have configure the swap memory only for the current session. 
  6. To make the swap memory configuration permanent execute the below commands
    sudo cp /etc/fstab /etc/fstab.bak
    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Now, swap memory is ready to use. By using the swap memory we can avoid the memory problems like "Out of memory: Kill process or sacrifice child"


Read Next: serializers in Django Rest Framework

Read Prev: renderers in Django Rest Framework

Blog Archive