Adjust Linux swapability

Introduction

Sometimes the server speed is somehow slow, so when I checked the status using top, ps, etc., it seemed that I was using swap.

Swap:          1.0G        278M        742M

I was wondering if it would be better to increase the memory, but the whole free command looked like this.

onodes@Balthazar:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:           985M        309M        311M         33M        364M        495M
Swap:          1.0G        278M        742M

As you can see from the available, 278MB of swap in occurred while the available amount of physical memory was sufficient.

What is a swap?

Swap here is a function to move the data in the memory to a disk (HDD / SSD, etc.) when the physical memory is insufficient. In other words, the idea is that swap will not occur unless there is insufficient physical memory.

swappiness Swappiness is set as a background for swap to be used even though physical memory is free. swappiness is a parameter of the Linux kernel, which is used to change and adjust the frequency of swap processing. It is implemented and adopted in Linux with Linux kernel 2.6 or higher (I think it seems to be the case in the world ...).

onodes@Balthazar:~$ cat /proc/sys/vm/swappiness
60

If unadjusted, 60 should be included by default. This value can be adjusted from 0 to 100, and the larger the value, the easier it is to swap. Also, if you set it to 0, swap will not be used until the memory is exhausted.

value frequency
swappiness = 0 Do not swap until memory is full (depleted)
swappiness = 60 Default
swappiness = 100 Swap positively. Level that affects overall performance

Looking only here, it seems that setting swappiness = 0 will improve performance by using memory hard, but if it is set to 0, OOM Killer will easily occur this time and process down will occur, so do not overdo it.

This time, let's relax a little and set swappiness = 10.

Change and reflection of swappiness

The OS is Ubuntu 18.04.

$ sudo vim /etc/sysctl.conf

Added at the bottom

vm.swappiness = 10

And reflect

$ sudo sysctl -p
vm.swappiness = 10

Confirmation and swap release

Hit free.

onodes@Balthazar:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:           985M        384M        181M         56M        419M        396M
Swap:          1.0G        276M        744M

The value of swap hasn't changed ... swap isn't released here. ** If the free space in real memory is larger than the capacity of swap used **, turn swap off, free it, and then turn it on again. If the real memory is low, stop various processes to make space in the real memory. If the memory is used effectively to that extent, I feel that the work itself of this article is unnecessary.

result

onodes@Balthazar:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:           985M        578M         95M         34M        311M        224M
Swap:          1.0G          0B        1.0G

It is 0B because it is just after releasing the swap. From here, let's follow up.

Recommended Posts

Adjust Linux swapability
Linux
linux memorandum
Linux command # 4
Linux commands
Linux commands
Linux basics
direnv (linux)
Organize Linux
linux commands
Linux practice
Ubuntu Linux 20.04
Linux Summary
Linux process
Linux permissions
Linux command # 5
About Linux
Forgot linux
About Linux
Linux commands
Linux commands
About Linux
About Linux
About Linux ①
Linux redirect
Adjust file permissions with the Linux command chmod