Linux decrease swap size

Change swap size in Ubuntu 18.04 or newer

Swap is a special area on your computer, which the operating system can use as additional RAM.
Starting with Ubuntu 17.04, the swap partition was replaced by a swap file. The main advantage of the swap file is easy resizing.

Note: before running the following commands, please make sure you have a backup of your data!

In the following example, we’ll extend the swap space available in the /swapfile from 4 GB to 8 GB.

sudo dd if=/dev/zero of=/swapfile bs=1G count=8

if = input file
of = output file
bs = block size
count = multiplier of blocks

4. Make the file usable as swap

6. Edit /etc/fstab and add the new swapfile if it isn’t already there

/swapfile none swap sw 0 0

7. Check the amount of swap available

grep SwapTotal /proc/meminfo

Share this:

Like this:

52 Comments

Hi, I already had a swapfile but reduced the size with your method. The last two lines of the fstab file read /dev/mapper/vgubuntu-swap_1 none swap sw 0 0
/swapfile swap swap defaults 0 0 Is this fine, or should I change this?

Hey dear
I was facing a freezing problem and i started follow this process but the freezing problem can’t allow me even type this commands how can i solve this?
Can someone please help me what should i do

Thank you ..Bogdan…..
besides the GREAT step by step!
That is exactly what I wanted to know to dual boot with Ubuntu and that other “thingy” that came with my laptop, (for warranty purposes only! LOL!) I have 12GB Ram, and so I’d use 24GB swap partition, so I can RUN “hibernation?
As I LOVE not using ANY battery as “sleep” might do! So the “swap file” only POINTS to the “swap partition” and is NOT actual “allocated space. ” I am a nooby, as you can TELL! LOL!

Yes! :), Exxcccatly according to this table! 🙂 RAM(GB) No hibernation With Hibernation Maximum
1 1 2 2
2 1 3 4
3 2 5 6
4 2 6 8
5 2 7 10
6 2 8 12
8 3 11 16
12 3 15 24
16 4 20 32
24 5 29 48
32 6 38 64
64 8 72 128
128 11 139 256

Hi Deep, For 8GB of physical RAM it’s recommended to have 8GB of swap space. If you’re also using hibernation, you should double the swap space to 16GB.
For more information, please see https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/storage_administration_guide/ch-swapspace#tb-recommended-system-swap-space

Thank you for the detailed instructions with explanation. This article solved my problem very quickly.

Bogdan, that’s an excellent article and thanks. A minor feedback: the default swap file name in Ubuntu 18.04 (Server) is /swap.img . Maybe it is helpful for readers if you describe how to see that by using cat /etc/fstab (or grep ”swap” /etc/fstab). In this case readers can skip Step 3 (to change the swap file permission) & 6 (to update the fstab file).

Hi Culip,
Thank you for your kind words and your feedback.
I don’t have an Ubuntu Server 18.04 installation available to check the default swap file name and I couldn’t find any reference in the official Ubuntu Server 18.04 docs
Skipping steps #3 and #6 if the file already exists is a great idea and I’ll update the article.

Читайте также:  Linux ставится windows нет

2 comments?
1.-if we stop swap and ,we do the file with dd, the server has lag
do you recomend make the swapfile first? 2.-after reboot the server the swap is 0% used, and all is setings is correct an my server show the correct new /swapfile as the new swap area. but is not used, fstab is correcto an the proc show te correct swapfile

Well, I answered my own question. I left entries for both the swap file and the swap partition in the fstab. When I restarted, both showed up as active, when I ran swapon -s. I’m not sure if having them both running concurrently could cause problems, so I commented out the entry for the swap partition and restarted. The box came back up with only the swap file running. That little 90mb swap partition can just sit there and gather dust. It doesn’t appear to be hurting anyone.

Hello. I recently moved onto an Ubuntu server v18.04 box, at a large webhost. The box was provisioned with a 90mb swap partition, which is really small and caused problems with a backup app. I walked through all of your commands and my server is now running a swap file, apparently bypassing the swap partition. The following lines are in the fstab: # swap was on /dev/sda6 during installation
UUID=25678faa-bdb3-4a22-b3ae-e6ef02d200f2 none swap sw 0 0 I added the swapfile line under it, but what should I do with the swap partition entry? Should I delete it or just let it initialize and sit dormant? Will running a swapfile in this situation cause problems? I really appreciate your information on resizing the swap space, BTW. It was well written and easy to follow.

Thank you for this. I’d been wanting to increase the swapfile size for a while, but every instruction set I found seemed to technical. Your instructions were clear, concise and did exactly what I needed. So thank you. 🙂

My fstab on ubuntu 20.04 has these entries – did not change any, do I still need to change ? I did follow all steps except any fstab change and this shows proper 8gb
grep SwapTotal /proc/meminfo
SwapTotal: 8388604 kB # /etc/fstab: static file system information.
#
# Use ‘blkid’ to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#
# / was on /dev/sdb3 during installation
UUID=0f2d2288-e7f1-4742-b53a-e466a50e91e7 / ext4 errors=remount-ro 0 1
# /home was on /dev/sdb4 during installation
UUID=b0651f1d-4dfd-46ff-9afd-964277a02e04 /home ext4 defaults 0 2
# swap was on /dev/sdb2 during installation
UUID=2f49d3f7-8691-40d8-b7a0-eaf71de5abf4 none swap sw 0 0

Hi Ionut,
I don’t think you need to change anything.
But you can check what type of swap is active (file or partition) with the “swapon -s” command. “-s” meaning summary.
This is what I get when I run this command on a machine with 4 GB of swap:
$ swapon -s
Filename Type Size Used Priority
/swap/file file 4194304 0 -2

[email protected]:~$ /swapfile none swap sw 0 0
bash: /swapfile: Permission denied swap is created but why show is permission denied when type /swapfile non swap sw 0 0 ?

Читайте также:  Linux вывести настройки сети

$ sudo vim /etc/fstab
(you can use any editor instead of vim)
and then paste this line
/swapfile none swap sw 0 0

Источник

Change Swap Size in Ubuntu

In the case of Linux, the swap file is an important part. It’s not a must-have option but having one is crucial for a number of purposes. Generally, when installing the system, you have to declare the size of the swap file. The recommended size is 4GB but you can either decrease or increase it depending on your demand. Today, let’s have a look at changing the swap size in Ubuntu.

Note – Running all these commands require root privilege, so I recommend running a “root” terminal.

Finding out available swap file(s)

Before we get to change the swap, let’s find out how much swap size we have.

According to the result, the system has a swap file at “/swapfile”.

For manipulating the swap file, we have to disable it first.

Now, change the size of the swap file –

Here, the total size of the swap file will be bs*count = 1M x 4096 = 4GB

Make the “/swapfile” usable again –

After restarting your system, check out the result –

Deleting the swap file

If your RAM space is high enough, then you probably want to get rid of the swap file. If that’s your case, then let’s get rid of the swap file!

Don’t worry! I’ll also show you how to set a completely fresh swap file.

For deleting the swap file, run the following commands –

Remove the swap file entry from “/etc/fstab” file. It should be the last line of the file.

Now, time to remove the actual swap file.

Don’t forget to restart your system!

For getting back the swap file, use the following commands –

# Adding permission where only root user can read the swap file

# Setup the file for swap space

Edit the “/etc/fstab” file and add the following line at the end of the file –

Voila! Your swap file is back!

What if you had a swap partition? You have to perform actions on the partition itself. That’s more troublesome than just simply managing your swap files directly using the methods above, right?

I recommend deleting the “swap” partition and use the above method to create a new swap file.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.

Источник

How to reduce size of swap after a system is already installed?

I’m running Debian Squeeze 6.0.5. Does the use of swap memory make my computer run slower? If so, how can I reduce the size of the swap memory after the system is already installed?

4 Answers 4

One doesn’t always want to reduce it, but often to increase its lazy usage instead — the more clean pages are already in swap, the better, it means they can easily be set off RAM when free RAM is needed. Linux VM, though, has some weird behavior regarding swapping — intensive disk I/O (like huge file cp ) can make your system swap unwanted heavily. It can be mitigated to some degree by decreasing vm.swappinness and increasing vfs_cache_pressure although the effect of such countermeasures isn’t always meeting expectations. I think it also makes sense to mention zswap here — for some workloads it can be useful.

Читайте также:  Проверка ошибок файловой системы линукс

@palik sysctl-explorer is a nice idea, but it uses outdated reference material; the kernel docs are more accurate.

To turn off swap temporarily, use (as root) the command:

To turn it off permanently, edit the file /etc/fstab and comment out any lines with swap in the type column.

More swap does not always improve performance. There are times when reducing it or turning it off is better — it all depends on the mix of applications.

I did a quick google and came up with this good article on the subject:

swap serves basically two purposes. It allows the system to continue to operate when physical memory runs out at a performance cost — run out of physical memory without it, you get crashes, lockups, and processes being killed with out of memory errors the second they ask for more memory than the system has. The reduced performance in this case is a symptom of being forced to use it for active processes, rather than a symptom of having too much swap.

It also lets physical memory be used more efficiently, by moving less-used pages in memory to disk until they are needed again. This frees up memory for cacheing purposes, which is usually a more efficient use of space than having infrequently used segments of program memory just sitting there locked in physical ram.

A long-standing best practice has been to size swap space at twice the physical memory, in other words, if you have 1GB of ram, devote 2GB to swap. This is still good advice, but in practice more modern systems with 4GB or more of physical ram can usually drop this to the same amount of swap as the system has of physical memory.

There are a few things you can do to improve performance when using swap. If you have multiple drives, moving swap to a faster or less used hard drive is recommended, and on a very IO-bound system, you may get significant performance increases by doing this. For traditional hard drives, moving swap closer to the center of the physical disk may help, as seek times are generally shorter at the center due to less travel of the drive heads.

Having swap on an SSD can help significantly as well, but I’d caution you that this may create a lot of wear and tear on a SSD, and will give it a shorter lifespan.

Of course, the best solution to improving memory performance is usually to throw more RAM into the box, and if you look at your memory usage and see heavy usage of swap along with little or no free memory, it’s a good indication that it’s time to invest in more ram.

Источник

Оцените статью
Adblock
detector