In this article, we will discuss what a swap file is, its types, and how to change the swap file size in Ubuntu. This is particularly useful for users who want to optimize their system performance by resizing the swap file based on their system’s requirements.

What is Swap?

Swap is a virtual memory management technique used by Linux systems, including Ubuntu, to temporarily store data that is not currently in use by the RAM. When the physical memory (RAM) is not sufficient to handle all the running processes, the operating system moves some data to the swap space, thereby freeing up RAM for other processes. Swap space can be created on the hard drive in the form of a swap file or a swap partition.

Types of Swap

There are two types of swap spaces in Linux systems:

  • Swap Partition: This is a dedicated partition on the hard drive created during the installation of the operating system. It is generally faster than a swap file and is recommended for systems with a high demand for swap space.
  • Swap File: This is a file created on an existing file system. It is simpler to manage and resize compared to a swap partition. Swap files are suitable for systems with lower swap space requirements or systems with limited disk space.

In this article, we will focus on resizing a swap file in Ubuntu.

Disable Existing Swap

Before resizing the swap file, you must first disable the existing swap. Follow these steps to disable the swap:

Open the terminal by pressing Ctrl Alt T or searching for “Terminal” in the application menu.

To disable the swap, enter the following command:

sudo swapoff -a 

Create and Use a New Swap File

To create a new swap file with a different size, follow the steps below:

  1. Remove the existing swap file:
    sudo rm /swapfile 
    
  2. Create a new swap file with your desired size (in this example, we will create a 4GB swap file):
    sudo fallocate -l 4G /swapfile 
    

    If the fallocate command is not available on your system, you can use dd instead:

    sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 
    
  3. Set the appropriate file permissions for the new swap file:
    sudo chmod 600 /swapfile 
    
  4. Format the new swap file:
    sudo mkswap /swapfile 
    
  5. Enable the new swap file:
    sudo swapon /swapfile 
    
  6. To make the new swap file available at system startup, edit the /etc/fstab file:
    sudo nano /etc/fstab 
    

    Add the following line at the end of the /etc/fstab file:

    /swapfile none swap sw 0 0

  7. Save and close the file by pressing Ctrl X, followed by Y and then Enter.
  8. Reboot your system to apply the changes:
    sudo reboot 
    

    After rebooting, the new swap file will be active with the desired size. You can verify this by running the following command:

    free -h 
    

Conclusion

In this article, we discussed how to change the swap file size in Ubuntu. We covered the basics of swap space, its types, disabling the existing swap, and creating and using a new swap file. Resizing the swap file can help optimize system performance based on your specific requirements.