How to find what is using linux swap or what is in the swap?
I have virtual linux (Fedora 17) server with 28GB RAM and 2GB swap. The server is running a MySQL DB that is set up to use most of the RAM. After some time running the server starts to use swap to swap out unsued pages. That is fine as my swappiness is at default 60 and it is the expected behaviour. The strange thing is that the number in top/meminfo does not correspond with info from processes. I.e. the server is reporting these numbers:
/proc/meminfo: SwapCached: 24588 kB SwapTotal: 2097148 kB SwapFree: 865912 kB top: Mem: 28189800k total, 27583776k used, 606024k free, 163452k buffers Swap: 2097148k total, 1231512k used, 865636k free, 6554356k cached
If I use the script from https://serverfault.com/a/423603/98204 it reports reasonable numbers (few MBs swapped by bash’es, systemd, etc) and one big allocation from MySQL (I omitted a lot of output lines):
892 [2442] qmgr -l -t fifo -u 896 [2412] /usr/libexec/postfix/master 904 [28382] mysql -u root 976 [27559] -bash 984 [27637] -bash 992 [27931] SCREEN 1000 [27932] /bin/bash 1192 [27558] sshd: admin@pts/0 1196 [27556] sshd: admin [priv] 1244 [1] /usr/lib/systemd/systemd 9444 [26626] /usr/bin/perl /bin/innotop 413852 [31039] /usr/libexec/mysqld --basedir=/usr --datadir=/data/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/data/mysql/err --open-files-limit=8192 --pid-file=/data/mysql/pid --socket=/data/mysql/mysql.sock --port=3306 449264 Total Swap Used
- The script output is not in KB. Even if it would be in 512 or 4KB units it won’t match. Actually the ratio (1200:440) is about 3:1 which is «strange» number.
- There are some pages in swap that are somehow shared between processes as mentioned in https://serverfault.com/a/477664/98204 . If this is true how can I find the actual number of memory used like this? I mean it would need to make cca 800MB difference. And that does not sound right in this scenario.
- There are some «old» pages in swap used by processes that already finished. I would not mind that if I were able to find out how much is this «freeable» swap.
- There are pages in swap that has been swapped back to memory and are in swap just in case they did not change in RAM and need to be swapped out again as mentioned in https://serverfault.com/a/100636/98204 . But the SwapCached value is only 24MB.
The strange thing is that the swap usage is slowly increasing while the sum output from the script is roughly the same. In last 3 days the swap used increased from 1100MB to current 1230MB while the sum increased from 430MB to current 449MB (ca.).
The server has enough free(able) RAM so I could just turn off the swap and turn it back on. Or I could probably set swappiness to 0 so the swap would get used only if the is no other way. But I would like to solve the issue or at least find out what is the cause of this.
How to display processes using swap space
Identify and print processes using swap space to get a better understanding of the Linux operating system.
Display processes using swap space
Use the following command to simply display processes using swap space. This list will be sorted by process id by default due to a way find command returns its results, which are parsed by awk utility.
$ find /proc -maxdepth 2 -path "/proc/6*/status" -readable -exec awk -v FS=":" 'END ' '<>' \;
[..] 11224 bash 520 kB 11372 chrome 4124 kB 11997 python2 1376 kB 14831 chrome 4296 kB 20457 chrome 4580 kB 20463 cat 92 kB 20464 cat 92 kB 20467 chrome 5204 kB 20468 nacl_helper 420 kB 20471 chrome 5100 kB 20587 chrome 12212 kB 20629 chrome 8224 kB [..]
Display processes using swap space sorted by used space
Use additional awk instance to add a temporary column at the beginning, so data could be easily sorted by used swap space in ascending order.
$ find /proc -maxdepth 2 -path "/proc/2*/status" -readable -exec awk -v FS=":" 'END ' '<>' \; | awk '' | sort -h | cut -d " " -f2-
[..] 20587 chrome 12212 kB 2098 firefox 12588 kB 2080 applet.py 13072 kB 10801 Web Content 15796 kB 21412 atom 17384 kB 1629 cinnamon 18584 kB 1300 Xorg 22048 kB 28740 atom 22692 kB 21482 atom 32800 kB 28761 atom 51644 kB 21444 atom 68044 kB 21432 atom 77080 kB
Display top ten processes using swap space
The following command will sort processes by used swap space in descending order, then execute head utility to limit the number of records.
$ find /proc -maxdepth 2 -path "/proc/4*/status" -readable -exec awk -v FS=":" 'END ' '<>' \; | awk '' | sort -hr | head | cut -d " " -f2-
21432 atom 77080 kB 21444 atom 67596 kB 28761 atom 51644 kB 21482 atom 32800 kB 28740 atom 22692 kB 1300 Xorg 22048 kB 1629 cinnamon 18584 kB 21412 atom 17384 kB 10801 Web Content 15796 kB 2080 applet.py 13072 kB
Display top ten processes using swap space with percentage values
Read (see 1st command) or calculate (see 2nd command) total available swap space to calculate and display per-process percentage swap usage. Both of these commands are equivalent.
$ find /proc -maxdepth 2 -path "/proc/4*/status" -readable -exec awk -v FS=":" -v TOTSWP="$(cat /proc/meminfo | sed -n -e "s/^SwapTotal:[ ]*\(9*\) kB/\1/p")" 'END >' '<>' \; | awk '' | sort -hr | head | cut -d " " -f2-
$ find /proc -maxdepth 2 -path "/proc/7*/status" -readable -exec awk -v FS=":" -v TOTSWP="$(cat /proc/swaps | sed 1d | awk 'BEGIN END')" 'END >' '<>' \; | awk '' | sort -hr | head | cut -d " " -f2-
21432 atom 77080 kB 0.93% 21444 atom 67596 kB 0.82% 28761 atom 51644 kB 0.62% 21482 atom 32800 kB 0.40% 28740 atom 22692 kB 0.27% 1300 Xorg 22048 kB 0.27% 1629 cinnamon 18584 kB 0.22% 21412 atom 17384 kB 0.21% 10801 Web Content 15796 kB 0.19% 2080 applet.py 13072 kB 0.16%
You can extend this idea further by creating bar charts in terminal. awk is beautiful!
Follow me on Mastodon , check out source code ad GitHub
Linux swap: what it is and how to use it
I mention Linux swap and swappiness in almost every article on things to do after installing Linux. You know that Linux swap is somehow related to RAM and swappiness can affect your system performance. It is time to explain what it is and how to use it in little more details.
Video
What is Linux Swap?
The Linux Kernel divides RAM into chunks of memories and the swapping process is when the Linux Kernel uses a hard disk space (swap space) to store information from RAM and thus releases some RAM space. That is why when you install a Linux distribution, the installation wizard usually asks you to assign some space for the system and another for the swap.
Using swap is a very useful way to extend the RAM because it provides the necessary additional memory when the RAM space has been exhausted and a process has to be continued. It is especially recommended when you have less than 1Gb of RAM. Although in the end, everything depends on you.
Do you need Linux Swap?
This is a question many novice users ask themselves when they begin to discover Linux. In fact, this will depend on the use and amount of RAM your computer has. Regarding the use, there are processes and applications that really use a lot of memory, for example, Google Chrome. However, most of the current equipment comes with at least 8Gb of RAM and that makes the swap process less necessary. Nevertheless, having a swap space is desirable even if you have a lot of RAM.
For example, usually, when your RAM gets full and the Linux kernel has no space to write into, your system will crash. On the other hand, if you have a swap space, it will be used by the Linux kernel and your system will keep working, though much slower. So, it is safer to have swap space.
Note: swap space has one disadvantage — it is much slower than RAM. So, adding a swap space will not make your computer faster, it will only help to overcome some limitations posed by RAM size.
Linux Swap Partition
I recommend that you create the swap partition during the installation of your Linux distribution. In general, these are the recommended sizes for the swap partition.
- If your computer has 1Gb of RAM or less, then the swap partition should be twice the size of the RAM.
- But, If you have between 2gb and 4gb of RAM, the size of the swap partition should be half the RAM.
- Finally, If you have more than 4gb of RAM, then it is enough to have 2Gb.
But everything depends on your use case.
You can check the type and size of your swap with this command:
Above, I have a swap partition of 2Gb.
Linux Swap File
Alternatively, you can create a Linux Swap File after the installation. The modern Linux Kernel allows Swapping to a swap file instead of a swap partition. A swap file has an advantage over a swap partition that you can change the size of your swap any time easily by changing a swap file size.
If you want to create a swap file, run this command first:
sudo fallocate -l 1G /swapfile
Note: this command is to create a 1Gb swap file. Replace 1G with the value you want.
Next, you have to set the correct permissions.
Then, format the file to swap.
If you want the changes to be permanent, you need to edit the /etc/fstab file and add the following.
/swapfile swap swap defaults 0 0
In the end, check the status of the swapfile:
If you see the size of a swap file in the total column, you have done everything correctly.
How to remove a Linux Swap File
In case you need to remove a Linux swap file for any reason, you need to follow these steps.
First, deactivate the swap.
If you created the entry in the /etc/fstab file, remove it. To remind you, it is the line: /swapfile swap swap defaults 0 0 entry.
Finally, delete the actual Linux Swap File.
How to adjust the Swappiness value
Swappiness is a property of the Linux Kernel to define how often the swap space will be used. As you know RAM is faster than a hard drive. So, every time you need to use swap, you will notice that some processes and applications will run slower. However, you can adjust the system to use much more RAM than swap. This can help improve overall system performance.
Normally, the default swappiness value is 60. The smaller this value, the more of your RAM will be used.
To verify the swappiness value, run this command:
You should see the value of 60.
If you want to modify the default value, you need to edit the file /etc/sysctl.conf .
And add the following (10 is the most commonly recommended value):
Save the file and close it with Ctrl+O and Ctrl+X shortcuts. In order to apply the changes, you need to reboot the system.
This way your Linux kernel will use more RAM and less swap, but it still will swap when your RAM memory gets critically full. Usually, this setting is recommended when you have more than 4Gb of RAM.
Conclusion
In conclusion, it is safer to have some swap space on your computer. You can use either a swap partition or a swap file. The latter becomes more and more common.
Linux swap is a technical concept but knowing how it works can improve the performance of the system. Just play with the swappiness value.
Do you have anything to add about Linux swap? Have you changed the default value of swappiness? Let me know in the comments.
Average Linux UserFollow I am the founder of the Average Linux User project, which is a hobby I work on at night. During the day I am a scientist who uses computers to analyze genetic data.