- How To Check Swap Usage Size and Utilization in Linux
- Check swap usage size and utilization in Linux
- How to Check Swap Space in Linux using /proc/swaps file
- Look for swap space in Linux using swapon command
- Use free command to monitor swap space usage
- See swap size in Linux using vmstat command
- top/atop/htop/glances command
- Linux Find Out What Process Are Using Swap Space
- Linux GUI tool to monitor swap space size and usage
- Conclusion
- Linux Find Out What Process Are Using Swap Space
- Finding out process ID and swap usage
- Listing all process swap space usage
- Say hello to smem
- Installation
- How do I use smem command?
- Options
- A note about top command
- Linux swap: what it is and how to use it
- Video
- What is Linux Swap?
- Do you need Linux Swap?
- Linux Swap Partition
- Linux Swap File
- How to remove a Linux Swap File
- How to adjust the Swappiness value
- Conclusion
How To Check Swap Usage Size and Utilization in Linux
H ow do I check swap (paging) usage under Linux operating systems using command bash/ksh line options? How do I check swap usage size on Linux operating system?
Swap space (also known as paging) is nothing but computer memory management involving swapping regions of memory to and from storage. You can see swap usage summary by device using any one of the following commands. You may have to login as root user to use the following commands.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | None |
Est. reading time | 5m |
The maximum useful size of a swap area depends on the architecture and the kernel version. For Linux kernels after v2.3.3+ there is no such limitation on swap size.
Check swap usage size and utilization in Linux
The procedure to check swap space usage and size in Linux is as follows:
- Open a terminal application.
- To see swap size in Linux, type the command: swapon -s .
- You can also refer to the /proc/swaps file to see swap areas in use on Linux.
- Type free -m to see both your ram and your swap space usage in Linux.
- Finally, one can use the top or htop command to look for swap space Utilization on Linux too.
How to Check Swap Space in Linux using /proc/swaps file
Type the following cat command to see total and used swap size:
# cat /proc/swaps
Sample outputs:
Another option is to type the grep command as follows:
grep Swap /proc/meminfo
Look for swap space in Linux using swapon command
Type the following command to show swap usage summary by device
# swapon -s
Sample outputs:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Use free command to monitor swap space usage
Use the free command as follows:
# free -g
# free -k
# free -m
Sample outputs:
See swap size in Linux using vmstat command
Type the following vmstat command:
# vmstat
# vmstat 1 5
Sample outputs:
Note down the following output from swap field:
- si: Amount of memory swapped in from disk (/s).
- so: Amount of memory swapped to disk (/s).
top/atop/htop/glances command
Type the following commands:
# atop
# htop
# top
# glances
Sample outputs from top command:
Sample outputs from htop command:
Fig.01: Linux: Swap Memory Usage Command
Linux Find Out What Process Are Using Swap Space
Try smem command:
smem
OR
top
Linux GUI tool to monitor swap space size and usage
Try Gnome or KDE system monitor tool. For example, the GNOME System Monitor shows you what programs are running and how much processor time, memory (including paging/swap space size), and disk space are being used.
Conclusion
This page showed you how to check for swap space size and utilization in Linux. If you see a large percentage of the swap space utilization, then it is time to add more physical RAM to the Linux system. Another option is to increase swap space by adding a swap file on Linux. Please see the following resources for more info:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Linux Find Out What Process Are Using Swap Space
T he top and free command display the total amount of free and used physical and swap memory in the server. How do I determine which process is using swap space under Linux operating systems? How do I find out swap space usage of a particular process such as memcached?
Tutorial details | |
---|---|
Difficulty level | Intermediate |
Root privileges | Yes |
Requirements | Linux with smem and /proc |
Est. reading time | 6 minutes |
You can use the any one of the following techniques but keep in mind that because of shared pages, there is no reliable way to get this information [ 1]
- /proc/meminfo – This file reports statistics about memory usage on the system. It is used by free to report the amount of free and used memory (both physical and swap) on the system as well as the shared memory and buffers used by the kernel. You can also use free, vmstat and other tools to find out the same information.
- /proc/$
/smaps , /proc/$ /status , and /proc/$ /stat : Use these files to find information about memory, pages and swap used by each process using its PID. - smem – This command (python script) reports memory usage with shared memory divided proportionally.
Finding out process ID and swap usage
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
To see swap space used by memcached (PID # 48440), enter (number #2):
# grep —color VmSwap /proc/48440/status
Sample outputs (number #4):
Or the following awk command (number #3):
# awk ‘/VmSwap/
Sample outputs (number #4):
Fig.01: Finding out memcached process swap usage on Linux
Listing all process swap space usage
Type the following bash for loop command to see swap space usage per process:
Type the following command to sort out output:
Say hello to smem
The smem command reports physical memory usage, taking shared memory pages into account. Unshared memory is reported as the USS (Unique Set Size). Shared memory is divided evenly among the processes sharing that memory. The unshared memory (USS) plus a process’s proportion of shared memory is reported as the PSS (Proportional Set Size). The USS and PSS only include physical memory usage. They do not include memory that has been swapped out to disk. Memory can be reported by process, by user, by mapping, or system-wide. Both text mode and graphical output are available.
Installation
To install smem [ 2] type the following command under Debian / Ubuntu Linux:
$ sudo apt-get install smem
RHEL / CentOS Linux user type the following command:
$ wget https://www.selenic.com/smem/download/smem-1.4.tar.gz
$ tar xvf smem-1.4.tar.gz
# cp /tmp/smem-1.2/smem /usr/local/bin/
# chmod +x /usr/local/bin/smem
How do I use smem command?
To see basic process information, enter:
# smem
Sample outputs:
To see library-oriented view, enter:
# smem -m
To see user-oriented view, enter:
# smem -u
Sample outputs:
To see systemwide memory usage summary pass the -w option:
# smem -w
Sample outputs:
To see system view
# smem -R 8G -K /path/to/vmlinux/on/disk -w
To see totals and percentages, enter:
# smem -t -p
Sample outputs:
Options
Type the following command to see all other supported options:
# smem —help
Sample outputs:
A note about top command
Type the top command as root:
# top
To sort process as per swap page usage (SWAP = VIRT – RES) type capital O (option) followed by p (small p ) and [Enter] key:
Fig.02 top command – sored process by swap usage (click to enlarge)
References:
It is not possible to get the exact size of used swap space of a process. The top command fakes this information by making SWAP = VIRT – RES, but that is not a good metric, because other stuff such as video memory counts on VIRT as well (for example: top says my X process is using 81M of swap, but it also reports my system as a whole is using only 2M of swap. Therefore, I will not add a similar Swap column to htop because I don’t know a reliable way to get this information (actually, I don’t think it’s possible to get an exact number, because of shared pages).
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Linux swap: what it is and how to use it
If you are a Linux user you have definitely heard about Linux Swap, but you may not know what it is exactly or have only superficial knowledge about it. Time to change that!
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:
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.
Finally, enable the swap.
If you want the changes to be permanent, you need to edit the /etc/fstab file and add the following.
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.
Источник