List swap devices linux

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:

  1. Open a terminal application.
  2. To see swap size in Linux, type the command: swapon -s .
  3. You can also refer to the /proc/swaps file to see swap areas in use on Linux.
  4. Type free -m to see both your ram and your swap space usage in Linux.
  5. 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:

  1. si: Amount of memory swapped in from disk (/s).
  2. 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

Источник

List swap devices linux

This page provides an introduction to swap space and paging on GNU/Linux. It covers creation and activation of swap partitions and swap files.

Linux divides its physical RAM (random access memory) into chunks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Support for swap is provided by the Linux kernel and user-space utilities from the util-linux package.

Contents

Swap space

Swap space can take the form of a disk partition or a file. Users may create a swap space during installation or at any later time as desired. Swap space can be used for two purposes, to extend the virtual memory beyond the installed physical memory (RAM), and also for suspend-to-disk support.

If it is beneficial to extend the virtual memory with swap depends on the amount of installed physical memory. If the amount of physical memory is less than the amount of memory required to run all the desired programs, then it may be beneficial to enable swap. This avoids out of memory conditions, where the Linux kernel OOM killer mechanism will automatically attempt to free up memory by killing processes. To increase the amount of virtual memory to the required amount, add the necessary difference (or more) as swap space.

The biggest drawback of enabling swap is its lower performance, see section #Performance. Hence, enabling swap is a matter of personal preference: some prefer programs to be killed over enabling swap and others prefer enabling swap and slower system when the physical memory is exhausted.

To check swap status, use:

Or to show physical memory as well as swap usage:

Swap partition

The factual accuracy of this article or section is disputed.

A swap partition can be created with most GNU/Linux partitioning tools. Swap partitions are typically designated as type 82 . Even though it is possible to use any partition type as swap, it is recommended to use type 82 in most cases since systemd will automatically detect it and mount it (see below).

To set up a partition as Linux swap area, the mkswap(8) command is used. For example:

To enable the device for paging:

To enable this swap partition on boot, add an entry to /etc/fstab :

where the device_UUID is the UUID of the swap space.

See fstab for the file syntax.

Activation by systemd

systemd activates swap partitions based on two different mechanisms. Both are executables in /usr/lib/systemd/system-generators . The generators are run on start-up and create native systemd units for mounts. The first, systemd-fstab-generator , reads the fstab to generate units, including a unit for swap. The second, systemd-gpt-auto-generator inspects the root disk to generate units. It operates on GPT disks only, and can identify swap partitions by their type GUID, see systemd#GPT partition automounting for more information.

Disabling swap

To deactivate specific swap space:

Alternatively use the -a switch to deactivate all swap space.

Since swap is managed by systemd, it will be activated again on the next system startup. To disable the automatic activation of detected swap space permanently, run systemctl —type swap to find the responsible .swap unit and mask it.

Swap file

As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).

Manually

Swap file creation

Use dd to create a swap file the size of your choosing. For example, creating a 512 MiB swap file:

Set the right permissions (a world-readable swap file is a huge local vulnerability):

After creating the correctly sized file, format it to swap:

Activate the swap file:

Finally, edit the fstab configuration to add an entry for the swap file:

For additional information, see fstab#Usage.

Remove swap file

To remove a swap file, it must be turned off first and then can be removed:

Finally remove the relevant entry from /etc/fstab .

Automated

zram-generator

The aim of this tool is the creation of zram devices. It is written in Rust and resides in systemd’s GitHub. It can be installed with the zram-generator package. Configuration is straightforward and explained in the README.

systemd-swap

systemd-swap is a script for creating hybrid swap space from zram swaps, swap files and swap partitions. It is not affiliated with the systemd project.

Install the systemd-swap package. Uncomment and set swapfc_enabled=1 in the Swap File Chunked section of /etc/systemd/swap.conf . Start/enable the systemd-swap service.

Visit the authors GitHub page for more information and setting up the recommended configuration.

Swap encryption

Performance

Swap operations are usually significantly slower than directly accessing data in RAM. Disabling swap entirely to improve performance can sometimes lead to a degradation, since it decreases the memory available for VFS caches, causing more frequent and costly disk I/O.

Swap values can be adjusted to help performance:

Swappiness

The swappiness sysctl parameter represents the kernel’s preference (or avoidance) of swap space. Swappiness can have a value between 0 and 200 (max 100 if Linux /sys/fs/cgroup/memory/memory.swappiness or /proc/sys/vm/swappiness can be read in order to obtain the raw integer value.

To temporarily set the swappiness value:

To set the swappiness value permanently, create a sysctl.d(5) configuration file. For example:

To test and more on why this may work, take a look at this article.

VFS cache pressure

Another sysctl parameter that affects swap performance is vm.vfs_cache_pressure , which controls the tendency of the kernel to reclaim the memory which is used for caching of VFS caches, versus pagecache and swap. Increasing this value increases the rate at which VFS caches are reclaimed[1]. For more information, see the Linux kernel documentation.

Priority

If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. The system will use swap areas of higher priority before using swap areas of lower priority. For example, if you have a faster disk ( /dev/sda ) and a slower disk ( /dev/sdb ), assign a higher priority to the swap area located on the fastest device. Priorities can be assigned in fstab via the pri parameter:

Or via the —priority parameter of swapon:

If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.

Using zswap or zram

Zswap is a Linux kernel feature providing a compressed write-back cache for swapped pages. This increases the performance and decreases the IO-Operations. ZRAM creates a virtual compressed Swap-file in memory as alternative to a swapfile on disk.

Striping

There is no necessity to use RAID for swap performance reasons. The kernel itself can stripe swapping on several devices, if you just give them the same priority in the /etc/fstab file. Refer to The Software-RAID HOWTO for details.

Источник

8 Useful Commands to Monitor Swap Space Usage in 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.

8 Commands to Check Linux Swap Space Usage

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

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.

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 the swapon Command

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

To view all devices marked as 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.

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

2. Using /proc/swaps which is equivalent to swapon

The /proc filesystem is a very special virtual filesystem in Linux. It 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.

3. Using ‘free’ Command

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.

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

4. Using top Command

The top command displays processor activity of your Linux system, tasks managed by 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 ‘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.

Htop Check Swap Usage

For more information regarding installation and usage about 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 installation and usage about glances command, read this article: Glances – An Advanced Real Time Linux System Monitoring Tool

8. Using the vmstat Command

This command is used to display information about virtual memory statistics. To install vmstat on your Linux system, you can read the article below and see more usage examples:

VmStat Check Swap Usage

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

  1. si: Amount of memory swapped in from disk (s).
  2. so: Amount of memory swapped to disk (s).

Summary

These are easy methods one can use and follow to monitor swap space usage 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.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

Читайте также:  Сканер mustek 1200 ub plus драйвер windows 10 x64
Оцените статью