- How to increase swap space?
- 9 Answers 9
- Resize Swap to 8GB
- Check if it worked
- Make it permanent (persist on restarts)
- GUI method for increasing the size of swap partition
- Case 1 — unallocated space present before or after the swap partition
- Case 2 — unallocated space is between the partitions
- Case 3 — if the unallocated space was present outside the Extended partition)
- How Much Swap Should You Use in Linux?
- What is swap? When is swap used?
- Why is swap needed?
- Do you need swap if you have lots of RAM?
- Can you use Linux without swap?
- How much should be the swap size?
- How much swap size do you use?
How to increase swap space?
I have, by default, 250MB of swap space in Ubuntu, and I want to expand it to a larger size. I need 800MB, which I think will be enough to open several applications without having to hit the current limit of swap. I hope someone can help me.
If your swap partition can be expanded into adjacent unallocated space then use gparted . If such space is not available, perhaps because your active (booted) partition is using all the non-swap space, then gparted will not allow you to decrease the size of the active partition in order to create unallocated space. In this case you can re-install Ubuntu in order to create a larger swap space.
9 Answers 9
You can always create a swap file to add more swap space. This is not the same in every aspect as swap partition, but it will be easy and dynamic.
In the following steps, change /media/fasthdd/swapfile.img to anything you like. For example, it can be /swap.img as well. /media/fasthdd/swapfile.img is just an example filename. If you are using this one, then of course there must be a directory /media/fasthdd/ with enough free space for your new swap file.
Use any terminal application to run the commands of the following steps. All commands should be run with root privileges. To do this, you can either add sudo to the beginning of every command or run sudo bash before running the commands.
- Create an empty file: This file will contain virtual memory contents so make file big enough for your needs. This one will create a 1GiB file, which means +1GiB swap space for your system:
dd if=/dev/zero of=/media/fasthdd/swapfile.img bs=1024 count=1M
mkswap /media/fasthdd/swapfile.img
# Add this line to /etc/fstab /media/fasthdd/swapfile.img swap swap sw 0 0
swapon /media/fasthdd/swapfile.img
If everything goes well, you should see that more swap space is available for use. You can use the following commands to check your new swap and confirm that it is active:
$ cat /proc/swaps Filename Type Size Used Priority /media/fasthdd/swapfile.img file 8388604 2724 -1 $ grep 'Swap' /proc/meminfo SwapCached: 4772 kB SwapTotal: 8388604 kB SwapFree: 8355812 kB
use the command «swapon -s» to see the status rather than «cat /proc/swaps». then the «free» command also gives swap usage info.
To edit /etc/fstab try this at the command line: sudo nano /etc/fstab .It is more safely, than sudo gedit /etc/fstab .
Resize Swap to 8GB
# Turn swap off # This moves stuff in swap to the main memory and might take several minutes sudo swapoff -a # Create an empty swapfile # Note that "1G" is basically just the unit and count is an integer. # Together, they define the size. In this case 8GB. sudo dd if=/dev/zero of=/swapfile bs=1G count=8 # Set the correct permissions sudo chmod 0600 /swapfile sudo mkswap /swapfile # Set up a Linux swap area sudo swapon /swapfile # Turn the swap on
Check if it worked
Make it permanent (persist on restarts)
Add this line to the end of your /etc/fstab :
/swapfile swap swap sw 0 0
/swapfile none swap sw 0 0 is preferable to this answer’s suggestion /swapfile swap swap sw 0 0 , as man fstab says (of the second field in an /etc/fstab line): «For swap partitions, this field should be specified as `none’.»
Great answer. If anyone is wondering whether they should use 8GB as the swap file size, check out the recommendations in the SwapFaq. TLDR: You should be OK matching the swap size to the amount of RAM you have. I originally had 8GB of RAM but only 2GB of swap size, and I was experiencing regular crashes due to OOM, so I increased my swap to 8GB, exactly as per this answer. I’m sure that’s pretty common.
GUI method for increasing the size of swap partition
Another way to increase the swap size is to use the GParted partition Editor. In short, you resize the swap partition, then right click on it and choose «Swapon».
It’s easier to boot gparted-live-disk or an Ubuntu live disk (so that the all /dev/sda partitions will be unmounted). If you run Ubuntu live disk in some versions before 18.04, you may need to install gparted by running the commands below:
sudo apt-get update sudo apt-get install gparted && sudo gparted
You must be able to increase the size of swap partition only if there is an unallocated space present before or after the swap partition. If there was no unallocated space (the space which we are trying to add with swap partition) below or above the swap partition, then we have to resize the partitions and get that unallocated space.
Case 1 — unallocated space present before or after the swap partition
In the above screenshot, I had 11.4 GB of unallocated space before the linux-swap and 12.8 GB after the swap partition.
- To resize, right click on the swap partition (/dev/sda9 here) and click on the Resize/Move option. It will look like this:
- Dragging the slider arrows left or right then click on the Resize/Move button. Your swap partition will be resized.
Case 2 — unallocated space is between the partitions
In the above screenshot, the unallocated space which we wants to add to the swap partition was between dev/sda7 and /dev/sda8 .To move the unallocated space from that to just above swap partition,we have to follow the simple steps given below,
- Right-click on the /dev/sda8 partition and click on Resize/Move option.
- click and move the slider to the extreme left,so that the unallocated space which was just above the /dev/sda8 partition will comes below /dev/sda8 .After that click Resize/Move button.
- And now the unallocated space was just above to the swap partition which was like Case 1.Now follow Case 1.
Case 3 — if the unallocated space was present outside the Extended partition)
I had an unallocated space of 18 GB just below to the extended partition.To add this space to the linux-swap partition(which was present inside the extended partition),we have to follow the below steps,
- Right-click on the extended partition and select Resize/Move option,it will be like the below screenshot
- Click and drag the arrow to the extreme right and click on Resize/Move,so that the unallocated space of 18 GB will comes at the bottom of extended partition.I had a 14.80 GB of unallocated space already present at the bottom and now the 18.34 GB combines with that to create unallocated space of (18.34+14.80 GB) at the bottom of the extended partition.
- Now there was an unallocated space just below to the swap partition,it will be like Case 1,then follow case 1.
NOTE: Don’t forget to take backup of all your important datas before proceeding the above operations.
How Much Swap Should You Use in Linux?
How much should be the swap size? Should the swap be double of the RAM size or should it be half of the RAM size? Do I need swap at all if my system has got several GBs of RAM? Your questions answered in this detailed article.
How much should be the swap size? Should the swap be double the RAM size, or should it be half of the RAM size? Do I need swap at all if my system has got several GBs of RAM? Perhaps these are the most commonly asked questions about choosing swap size while installing Linux. It’s nothing new. There has always been a lot of confusion around swap size. For a long time, the recommended swap size was double the RAM size but that golden rule is not applicable to modern computers anymore. We have systems with RAM sizes up to 128 GB; many old computers don’t even have this much hard disk. But what swap size would you allot to a system with 32 GB of RAM? 64GB? That would be a ridiculous waste of a hard disk, won’t it? Before you see how much swap size you should have, let’s quickly learn a thing or two about swap memory. This will help you understand why swap is used. The explanation has been simplified for (almost) everyone’s understanding.
What is swap? When is swap used?
Your system uses Random Access Memory (aka RAM) when it runs an application. When only a few applications are running, your system manages with the available RAM. But if too many applications are running or the applications need a lot of RAM, your system gets into trouble. If an application needs more memory, but the entire RAM is already in use, the application will crash. Swap acts as a breather to your system when the RAM is exhausted. What happens here is that when the RAM is exhausted, your Linux system uses part of the hard disk memory and allocates it to the running application. That sounds cool. Does this mean if you allocate 50GB of swap size, your system can run hundreds or perhaps thousands of applications simultaneously? WRONG! You see, the speed matters here. RAM access data in the order of nanoseconds. An SSD access data in microseconds, while a regular hard disk accesses the data in milliseconds. This means that RAM is 1000 times faster than SSD and 100,000 times faster than the usual HDD. If an application relies too much on the swap, its performance will degrade as it cannot access the data at the same speed as it would have in RAM. So instead of taking 1 second for a task, it may take several minutes to complete the same task. It will leave the application almost useless. This is known as thrashing in computing terms. In other words, a little swap is helpful. A lot of it will be of no good use.
Why is swap needed?
- If your system has RAM less than 1 GB, you must use swap as most applications would exhaust the RAM soon.
- If your system uses resource heavy applications like video editors, it would be a good idea to use some swap space as your RAM may be exhausted here.
- If you use hibernation, then you must add swap because the content of the RAM will be written to the swap partition. This also means that the swap size should be at least the size of RAM.
- Avoid strange events like a program going nuts and eating RAM.
Do you need swap if you have lots of RAM?
This is a good question indeed. If you have 32GB or 64 GB of RAM, chances are that your system would perhaps never use the entire RAM and hence it would never use the swap partition.
But will you take the chance? I am guessing if your system has 32GB of RAM, it should also be having a hard disk of 100s of GB. Allocating a couple of GB of swap won’t hurt. It will provide an extra layer of ‘stability’ if a faulty program starts misusing RAM.
Can you use Linux without swap?
Yes, you can, especially if your system has plenty of RAM. But as explained in the previous section, a little bit of swap is always advisable.
In fact, a distribution like Ubuntu automatically creates a swap file of 2 GB in size. This should give you an indication that having some swap space is recommended.
Don’t worry if you didn’t create a swap partition while installing Linux. You can always create swap file in Linux at any time.
How much should be the swap size?
Now comes the big question. What should be the ideal swap space for a Linux install?
And the problem here is that there is no definite answer to this swap size question. There are just recommendations.
Different people have a different opinion on ideal swap size. Even the major Linux distributions don’t have the same swap size guideline.
If you go by Red Hat’s suggestion, they recommend a swap size of 20% of RAM for modern systems (i.e. 4GB or higher RAM).
- Twice the size of RAM if RAM is less than 2 GB
- Size of RAM + 2 GB if RAM size is more than 2 GB i.e. 5GB of swap for 3GB of RAM
Ubuntu has an entirely different perspective on the swap size as it takes hibernation into consideration. If you need hibernation, a swap of the size of RAM becomes necessary for Ubuntu.
- If RAM is less than 1 GB, swap size should be at least the size of RAM and at most double the size of RAM
- If RAM is more than 1 GB, swap size should be at least equal to the square root of the RAM size and at most double the size of RAM
- If hibernation is used, swap size should be equal to size of RAM plus the square root of the RAM size
Confused? I know it is confusing. This is why I have created this table that will give you the Ubuntu recommended swap size based on your RAM size and hibernation need.
RAM Size | Swap Size (Without Hibernation) | Swap size (With Hibernation) |
---|---|---|
256MB | 256MB | 512MB |
512MB | 512MB | 1GB |
1GB | 1GB | 2GB |
2GB | 1GB | 3GB |
3GB | 2GB | 5GB |
4GB | 2GB | 6GB |
6GB | 2GB | 8GB |
8GB | 3GB | 11GB |
12GB | 3GB | 15GB |
16GB | 4GB | 20GB |
24GB | 5GB | 29GB |
32GB | 6GB | 38GB |
64GB | 8GB | 72GB |
128GB | 11GB | 139GB |
How much swap size do you use?
The answer is never simple. As I stated earlier, for a long time, swap has been recommended to be double the size of RAM. In fact my Dell XPS 13 Ubuntu edition has 16GB of swap size for 8GB of RAM. So even Dell decided to go with the golden rule of swap=2xRAM.
What swap size do you prefer for your Linux system?