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.

Advertisement

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 search 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. As you already have existing swap, So we are assuming that you must have the `/etc/fstab` file entry. Open `/etc/fstab` file and check for the below entry.

  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.

Share.

2 Comments

  1. it worked great for me as well … thanks for this great little but meaningful and helpful article here! 🙂

    and i have the same question too, because after i did all commands down to step 5, then i couldn’t find out how to do step 6 by finding that particular file, editing and then re-saving it … (it requires ‘root level’ permissions and stuff that are not easy for me to do right now even though i did spend some time searching around the web to learn how to do it … and someday i sure will of course!)

    so, i just did the reboot thing (step 8) and after the system restarted and i logged in, the new swap file size was there already when i checked it on System Monitor! 🙂 (no need to use the last command (step 9) via Terminal to find out how things are really …)

    on another device, i have to do this every single time i start the machine (cold boot or warm boot, no difference!) and enter a certain long command in Terminal manually, which works but is annoying if i forget to do so! (and has errors too!) now, i’m trying to do the same thing on that device (a laptop) as well and i’m hoping it’s going to work just the same as it did on my desktop system …

  2. Thank you for this good article, but I have a question.

    If the swapfile already exist (as it seems to be the case in your example), I think the step 6 is not needed, right ?

Leave A Reply

Exit mobile version