In this article, we will talk about what a swap file is, the different types of swap, and how to change the swap file size in Ubuntu. This is useful for people who want to make their computer run better by changing the size of the swap file.
What is Swap?
Swap is a way for Linux systems, like Ubuntu, to manage memory. It temporarily stores data that the computer’s RAM (memory) is not using right now. When the RAM is full and cannot handle all the running programs, the operating system moves some data to the swap space. This frees up RAM for other tasks. Swap space can be on the hard drive as 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 special part of the hard drive set up when you install the operating system. It is usually faster than a swap file and is better for computers that need a lot of swap space.
- Swap File: This is a file made on the existing file system. It is easier to manage and resize than a swap partition. Swap files are good for computers that do not need much swap space or have limited hard drive space.
In this article, we will focus on changing the size of a swap file in Ubuntu.
Disable Existing Swap
Before changing the swap file size, you need to turn off the existing swap. Follow these steps:
- Open the terminal by pressing Ctrl + Alt + T or search for “Terminal” in the application menu.
- To turn off the swap, type this command and press Enter:
sudo swapoff -a
Create and Use a New Swap File
To create a new swap file with a different size, follow these steps:
- Remove the existing swap file: Make sure to remove existing swap file
sudo rm /swapfile
- Create a new swap file with the size you want (in this example, we will create a 4GB swap file):
sudo fallocate -l 4G /swapfile
If the fallocate command does not work on your system, use this command instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
- Set the correct permissions for the new swap file:
sudo chmod 600 /swapfile
- Format the new swap file:
sudo mkswap /swapfile
- Turn on the new swap file:
sudo swapon /swapfile
- Update the swap file entry in /etc/fstab: Open the /etc/fstab file and make sure it has this line:
/swapfile none swap sw 0 0
- Save and close the file: Press Ctrl + X, then Y, and then Enter to save and close the file.
- Restart your computer to apply the changes:
sudo reboot
- After restarting, the new swap file will be active with the size you chose. To check this, use this command:
free -h
Conclusion
In this article, we talked about how to change the swap file size in Ubuntu. We explained what swap space is, the types of swap, how to turn off the existing swap, and how to create and use a new swap file. Changing the swap file size can help your computer run better based on what you need.
2 Comments
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 …
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 ?