7

This is what the free command reports on a server that is running a Spring Boot application behind an Nginx reverse proxy:

$ free -h

               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       893Mi       164Mi        29Mi       919Mi       883Mi
Swap:          511Mi       481Mi        30Mi

How should I interpret the high swap usage? Is that a sign that I should upgrade the server to a higher memory capacity (e.g. 4 GiB)?

Output of vmstat

$ vmstat 1 5

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0 493428 150620  95476 879668    0    1     8    28    1    0  3  1 95  0  1
 0  0 493428 150620  95476 879708    0    0     0     4  970 1891  4  1 95  0  0
 1  0 493428 150620  95476 879712    0    0     0     0  851 1708  6  6 86  0  3
 2  0 493428 150620  95476 879712    0    0     0     0  704 1398  4  5 91  0  0
 1  0 493428 150620  95476 879712    0    0     0    16  583 1206  3  3 94  0  0
0

2 Answers 2

4

As you can see from vmstat output you do not have si or so activities, even the average (first line) is negligible. Try to play with swappiness but if you do not see serious performance degradation you do not need to change anything.

2
  • 1
    Some context: "Swap ... si: Amount of memory swapped in from disk (/s). ... so: Amount of memory swapped to disk (/s)." Aug 15, 2022 at 17:33
  • 3
    The lack of swap activity suggests that the high swap usage could be from programs (or parts thereof) that got loaded and then got swapped out to disk because they were unneeded. For example, Nginx is being used as a reverse proxy, so the parts related to load balancing aren't being used and are a good candidate for preemptive swapping out to disk.
    – Mark
    Aug 15, 2022 at 23:38
4

In my opinion, this shouldn't be interpreted as an argument to do a RAM upgrade directly since there are many factors regarding SWAP, one being swappiness.

From Ubuntu FAQs:

What is swappiness and how do I change it?

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

  • swappiness can have a value of between 0 and 100
  • swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible
  • swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache

The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation. A value of swappiness=10 is recommended, but feel free to experiment. Note: Ubuntu server installations have different performance requirements to desktop systems, and the default value of 60 is likely more suitable.

To check the swappiness value

cat /proc/sys/vm/swappiness

To change the swappiness value, a temporary change (lost on reboot) with a swappiness value of 10 can be made with

sudo sysctl vm.swappiness=10

To make a change permanent, edit the configuration file with your favorite editor:

gksudo gedit /etc/sysctl.conf

Search for vm.swappiness and change its value as desired. If vm.swappiness does not exist, add it to the end of the file like so:

vm.swappiness=10

Save the file and reboot or enter the following:

sudo sysctl --load=/etc/sysctl.conf

There is a much better-explained post here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .