Linux top swap usage

How to Check Swap Memory in Linux: A Comprehensive Guide

Learn how to check swap memory utilization and usage in Linux using various commands, including free, swapon, /proc/swaps, top, vmstat, and sar.

  • Using the free command
  • Using other commands to monitor swap space usage
  • Linux Crash Course — Understanding Memory and Swap Usage
  • Checking swap space on the system
  • Checking swap usage for each process
  • Clearing swap space
  • Other quick code samples for checking Linux swap memory usage
  • Conclusion
  • How to check swap memory in Linux using top?
  • Where is swap memory in Linux?
  • What is the swap memory in Linux?
  • How much memory do I have for swap?

As a Linux user, you might have heard about swap memory. Swap memory is an essential part of Linux systems that helps manage memory usage when physical memory (RAM) is full. In this article, we will discuss various ways to check swap memory in Linux.

Using the free command

The free command is a popular utility that can be used to check memory and swap utilization on the system. It is available on most Linux distributions by default. To check swap memory using the free command, open a terminal window and type the following command:

The -m option is used to display the output in megabytes, making it easier to read. The output of the free command shows the total, used, and free swap memory.

 total used free shared buff/cache available Mem: 985 324 258 36 402 422 Swap: 1023 0 1023 

In the above output, we can see that the total swap memory is 1023 megabytes, and there is no swap memory currently being used.

Using other commands to monitor swap space usage

Apart from the free command, there are several other commands that can be used to monitor swap space usage.

The swapon command

The swapon command can be used to enable or disable swap space on the system. To enable swap space, use the following command:

$ sudo swapon /path/to/swapfile 

Replace /path/to/swapfile with the path to the swap file on your system. To disable swap space, use the following command:

$ sudo swapoff /path/to/swapfile 

The /proc/swaps file

The /proc/swaps file displays information about the swap space usage. To view the contents of the /proc/swaps file, type the following command:

The output will show the path of the swap file or partition along with the size, used, and priority.

Filename Type Size Used Priority /dev/sda2 partition 2097148 0 -2 

The top command

The top command shows the current swap space usage by each process. To launch the top command, type the following command:

Читайте также:  Как установить zmap на kali linux

Once the top command is launched, press the f key to select the fields to display. Use the arrow keys to move down to the SWAP field and press the d key to enable it. The top command will now display the swap space usage by each process.

The vmstat and sar commands

The vmstat and sar commands provide detailed information about the memory and swap usage. To use the vmstat command, type the following command:

The output will show the total swap space size and the amount of swap space used.

 2097148 K total swap space 0 K used swap space 2097148 K free swap space 1048576 K total high memory 350252 K used high memory 698324 K free high memory 

To use the sar command, type the following command:

The output will show the swap space usage for each minute.

Linux Crash Course — Understanding Memory and Swap Usage

Checking swap space on the system

To view the swap space on the system, use the swapon —show command. This command shows the path of the swap file or partition along with the size, used, and priority.

$ swapon --show NAME TYPE SIZE USED PRIO /dev/sda2 partition 2G 0B -2 

The location of the swap space can also be checked using the cat /proc/swaps or swapon -s command.

Checking swap usage for each process

The Swap column in /proc/[PID]/smaps shows the amount of swap space used by each process. To check the size and current usage of swap space, use the following command:

$ grep SwapTotal /proc/meminfo 

The output will show the total swap space size and the amount of swap space used.

SwapTotal: 2097148 kB SwapFree: 2097148 kB SwapCached: 0 kB 

Clearing swap space

Swap space can be cleared using the swapoff command. The command disables all swap space on the system.

Other quick code samples for checking Linux swap memory usage

In Shell , for instance, how to free swap memory in linux code sample

sudo swapoff -a; sudo swapon -a

In Shell , for example, how to check swap memory in linux code sample

Conclusion

Swap memory is an important aspect of Linux systems that helps manage memory usage when RAM is full. Various commands can be used to check swap memory utilization and usage in Linux. It is essential to monitor swap space usage and ensure that it does not affect system performance. Best practices suggest setting up appropriate swap space during the operating system installation and avoiding disabling it unless necessary. With the knowledge gained from this article, you can now effectively monitor and manage swap memory on your Linux system.

Источник

8 Useful Commands to Monitor Swap Space Usage in Linux

Linux memory management is an essential aspect of every System Administrator to improve the performance of a Linux system. It is always a good practice to monitor swap space usage in Linux to ensure that your system operates relative to its memory demands.

Check Linux Swap Space Usage

Therefore in this article, we are going to look at ways to monitor swap space usage in a Linux system.

What is Swap space?

Swap space is a restricted amount of physical memory that is allocated for use by the operating system when available memory has been fully utilized. It is memory management that involves swapping sections of memory to and from physical storage.

Читайте также:  Unpack tar bz2 linux

On most distributions of Linux, it is recommended that you set swap space when installing the operating system. The amount of swap space you can set for your Linux system may depend on the architecture and kernel version.

How Do I Check Swap Space Usage in Linux?

We shall look at different commands and tools that can help you to monitor your swap space usage in your Linux systems as follows:

1. Using swapon Command – Check Swap Usage

The swapon command helps you to specify the devices on which paging and swapping will be done and we shall look at a few important options.

To view all devices marked as a swap in the /etc/fstab file you can use the —all option. Though devices that are already working as swap space are skipped.

If you want to view a summary of swap space usage by device, use the —summary option as follows.

# swapon --summary Filename Type Size Used Priority /dev/sda10 partition 8282108 0 -1

Use —help option to view help information or open the manpage for more usage options.

Check Swap Usage in Linux

2. Using /proc/swaps – Measures Swap Space

The /proc filesystem is a very special virtual filesystem in Linux, which is also referred to as a process information pseudo-file system.

It actually does not contain ‘real’ files but runtime system information, for example, system memory, devices mounted, hardware configuration, and many more. Therefore you can also refer to it as a control and information base for the kernel.

To understand more about this filesystem read our article: Understanding /proc File System in Linux.

To check swap usage information, you can view the /proc/swaps file using the cat utility.

# cat /proc/swaps Filename Type Size Used Priority /dev/sda10 partition 8282108 0 -1

3. Using ‘free’ Command – Show Swap Usage

The free command is used to display the amount of free and used system memory. Using the free command with -h option, which displays output in a human-readable format.

# free -h total used free shared buffers cached Mem: 7.7G 4.7G 3.0G 408M 182M 1.8G -/+ buffers/cache: 2.7G 5.0G Swap: 7.9G 0B 7.9G

From the output above, you can see that the last line provides information about the system swap space. More usage and examples of free commands can be found at: 10 free Commands to Check Memory Usage in Linux.

4. Using top Command

The top command displays the processor activity of your Linux system, and tasks managed by the kernel in real-time. To understand how the top command works, read this article: 12 top Commands to Check Linux Process Activity

To check swap space usage with the help of the ‘top’ command run the following command.

Check Swap Space Using Top Command

5. Using atop Command

The atop command is a system monitor that reports about activities of various processes. But importantly it also shows information about free and used memory space.

Atop Check Swap Usage

To know more about how to install and use atop command in Linux, read this article: Monitor Logging Activity of Linux System Processes

6. Using htop Command

The htop command is used to view processes in an interactive mode and also displays information about memory usage.

Читайте также:  Расшифровка cap kali linux

Htop Check Swap Usage

For more information regarding the installation and usage of the htop command, read this article: Htop – Interactive Linux Process Monitoring

7. Using the Glances Command

This is a cross-platform system monitoring tool that displays information about running processes, cpu load, storage space usage, memory usage, swap space usage, and many more.

Glances Check Swap Usage

For more information regarding the installation and usage of the glances command, read this article: Glances – An Advanced Real-Time Linux System Monitoring Tool

8. Using the vmstat Command

The vmstat command is used to display information about virtual memory statistics, information about running processes, memory usage, CPU activity, paging, etc.

To install vmstat on your Linux system, run:

$ sudo apt install vmstat [On Debian, Ubuntu and Mint] $ sudo yum install vmstat [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] $ sudo emerge -a sys-apps/vmstat [On Gentoo Linux] $ sudo apk add vmstat [On Alpine Linux] $ sudo pacman -S vmstat [On Arch Linux] $ sudo zypper install vmstat [On OpenSUSE]

After vmstat installation, run:

VmStat Check Swap Usage

You need to take note of the following in the swap field from the output of this command.

  • si: Amount of memory swapped in from disk (s).
  • so: Amount of memory swapped to disk (s).
Summary

These are easy methods one can use and follow to monitor swap space usage in Linux and hope this article was helpful. In case you need help or want to add any information relating to memory management in Linux systems, please post a comment. Stay connected to Tecmint.

Источник

28 Июл 2020 18:07:11 | 2 комментария

Как быстро выяснить какой процесс в Linux использует пространство подкачки (swap)

Заметка очень короткая и призвана администраторам помочь быстро найти процессы которые максимально используют пространство swap. Что делать с этими процессами — это уже отдельная тема, главное найти кто потребляет swap.

Исходные данные: ОС Oracle Linux 7;
Задач: Найти потребителя SWAP

Типичная ситуация на сервере с системой мониторинга — это аларм вида:
prod-srv-01 Low free swap space (free: 0.15 %, threshold: 10%, alert started: 8.79 %)

Вначале немного теории, о том как получить информацию о распределении памяти процессами в Linux.

Мы видим, что swap заполнен на 100% — это плохо. Попробуем быстро выяснить кто основной потребитель, для этого обратимся к /proc/*/status
Ниже простой сценарий на bash который выдаст нам список потребителей swap:

for file in /proc/*/status ; do awk '/VmSwap|Name/END< print "">' $file; done | sort -k 2 -n -r | less

Мы видим, что основной потребитель — это процесс ora_j001_bs. На сервере установлен Oracle и один из процессов потребляет swap.
На втором месте мы видим процесс rsyslogd — думаю он в представлении не нуждается. Если на потребителя №1 мы не можем повлиять быстро, то на потребителя №2 (rsyslogd) можем — это попытаться его перазапустить. Выполняем перезапуск rsyslogd:

systemctl restart rsyslog
[[email protected] ~]# free -h total used free shared buff/cache available Mem: 15G 2.8G 301M 5.6G 12G 6.9G Swap: 5.0G 3.4G 1.6G

Мы видим, что стало доступно 1.6 GB, а это уже более 30% от размера swap, что вполне нас должно устроить на первое время. На этом все, до скорых встреч. Если у Вас возникли вопросы или Вы хотите чтобы я помог Вам, то Вы всегда можете связаться со мной разными доступными способами.

Источник

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