Managing and monitoring swap space on Linux (2024)

Swap space can play an important role in system performance. Learn how to determine how much swap space your system has available and how much it's being used.

Managing and monitoring swap space on Linux (1)

Credit: Sandra Henry-Stocker

Most of us don’t often think about swap space unless we run into a problem on our systems that suggests we don’t have enough. Even so, viewing and gauging the adequacy of swap space on a system is not overly complicated, and knowing what’s normal for your system can help you spot when something is wrong. So let’s check out some commands that can help you look into your swap space. But first, let’s review some fundamentals.

What swap space is and how it’s used

Swap space is disk space that acts something like an extension of memory. It gets used when the system’s physical memory (RAM) is full and the system needs more memory resources. It’s called “swap” because the system will move some inactive pages in memory into the swap space so that it can accommodate more data in RAM. In other words, it provides a way to free up RAM on a busy system.

Programs and data use RAM because that’s the only way they can be processed by the system. In fact, when a system boots, it moves programs like the kernel and systemd into RAM to get going.

Swap space might be configured as its own disk partition or be set up as a file. These days, most Linux installations create a partition during installation, and this is optimal. You can, however, set up a swap file and use it for your swap space.

With inadequate swap space, you can run into a problem called “thrashing” in which programs and data are moved between RAM and the swap space so frequently that the system runs very slowly.

Together, RAM and swap are referred to as “virtual memory.”

How much swap do you need?

The recommendation for swap space used to be double your RAM, but that was back when systems didn’t have as much RAM as they generally have today. These recommendations for Ubuntu should probably work well for other distributions as well:

RAM Swap Swap (with hibernation)256MB 256MB 512MB512MB 512MB 1GB1GB 1GB 2GB2GB 1GB 3GB3GB 2GB 5GB4GB 2GB 6GB6GB 2GB 8GB8GB 3GB 11GB12GB 3GB 15GB16GB 4GB 20GB24GB 5GB 29GB32GB 6GB 38GB64GB 8GB 72GB128GB 11GB 139GB

The distinction between swap and swap with hibernation is important. A system that hibernates saves your system state immediately to the hard disk and powers down. When you wake it up (e.g., by lifting the “lid” on a laptop), all the programs you were running return to the state they were in when the system went into hibernation. So, more swap space is recommended. Not all systems hibernate.

To determine if your system can hibernate, run this command:

$ which pm-hibernate/usr/sbin/pm-hibernate

If you get the response shown above, your system is hibernation-ready. You can test it by running this command:

$ sudo pm-hibernate

How you can view the amount of swap space on your Linux system?

You can use the swapon –show command to view the swap space on your system.

$ swapon --showNAME TYPE SIZE USED PRIO/dev/zram0 partition 5.8G 3.3M 100

Another useful command is the free command that displays both swap space and memory usage. With -m, the results are displayed in MBs instead of KBs.

$ free total used free shared buff/cache availableMem: 6064768 740736 538288 8060 4785744 5014712Swap: 6064124 3328 6060796$ free -m total used free shared buff/cache availableMem: 5922 723 525 7 4673 4897Swap: 5921 3 5918

The sar command can report on swap space usage.

$ sar -S 1 3Linux 5.13.9-200.fc34.x86_64 (dragonfly) 09/10/2021 _x86_64_ (2 CPU)02:09:55 PM kbswpfree kbswpused %swpused kbswpcad %swpcad02:09:56 PM 6060796 3328 0.05 0 0.0002:09:57 PM 6060796 3328 0.05 0 0.0002:09:58 PM 6060796 3328 0.05 0 0.00Average: 6060796 3328 0.05 0 0.00

Notice in the above output from the free command that swap space is being modestly used even though a lot of free memory is available.

You can also view a swap partition with a command like this:

$ lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTloop0 7:0 0 32.3M 1 loop /var/lib/snapd/snap/snapd/12704loop1 7:1 0 55.4M 1 loop /var/lib/snapd/snap/core18/2128loop2 7:2 0 65.4M 1 loop /var/lib/snapd/snap/powershell/173loop3 7:3 0 32.3M 1 loop /var/lib/snapd/snap/snapd/12883sda 8:0 0 111.8G 0 disk├─sda1 8:1 0 1G 0 part /boot└─sda2 8:2 0 110.8G 0 part /sdb 8:16 0 465.8G 0 disk└─sdb1 8:17 0 434G 0 part /homesdc 8:32 1 1.9T 0 disk└─sdc1 8:33 1 1.9T 0 partsr0 11:0 1 1024M 0 romzram0 252:0 0 5.8G 0 disk [SWAP] 

When you do and don't need more swap space

If your system has a lot of memory, you may never need to use swap space. But it's nearly always a good idea to have it available. Disk space is relatively cheap compared to memory, and you never know when some process might add to the burden. On the other hand, if your swap space is heavily used nearly all of the time, you should maybe consider adding more RAM to the system as there is some performance cost associated with its use.

Creating a swap file

If you need to create a swap file on a Linux system, use a command like this:

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=8192[sudo] password for me:8192+0 records in8192+0 records out8589934592 bytes (8.6 GB, 8.0 GiB) copied, 147.893 s, 58.1 MB/s

Once the file is created, change its file permissions, run the mkswap command and use the swapon -a command to make it available and the swapon --show command to verify that it has been put into use.

$ sudo chmod 600 /swapfile$ sudo mkswap /swapfileSetting up swapspace version 1, size = 8 GiB (8589930496 bytes)no label, UUID=3d060a1d-90d1-436f-97b6-4d1aebb15ce2$ sudo swapon -a$ swapon --showNAME TYPE SIZE USED PRIO/swapfile file 8G 0B -2

How to turn swapping off and back on again

It is possible to turn use of a swap file on and off and using the swapoff and swapon commands, though you'd probably only want to turn swapping off if you added a swap partition and want to use it instead of the swap file.

$ sudo swapoff -v /swapfileswapoff /swapfile$ sudo swapon -v /swapfileswapon: /swapfile: found signature [pagesize=4096, signature=swap]swapon: /swapfile: pagesize=4096, swapsize=8589934592, devsize=8589934592swapon /swapfile$ swapon –showNAME TYPE SIZE USED PRIO/swapfile file 8G 0B -2

Wrap-up

If your Linux system runs smoothly all of the time, it likely isn't having any problems with memory or swap. However, if it isn't or if you're just curious about how swap space is set up and used, try some of the commands above to see what they can tell you.

Related content

  • how-toTracking user logins on Linux Linux systems provide many ways to view user activity, including when they login, how long they stay logged in, and what commands they run.By Sandra Henry StockerSep 16, 20246 minsLinux
  • how-toBash command cheat sheet Developing bash scripts lets you do a lot of work with little or no effort. This bash command cheat sheet will help you get started.By Sandra Henry-StockerSep 13, 20246 minsLinux
  • newsWhy eBPF is critical and how it's getting better eBPF is a foundational Linux networking technology used for routing, monitoring and security, and soon it will be coming to Microsoft Windows, too.By Sean Michael KernerSep 12, 20245 minsLinuxNetworking
  • opinionDon’t miss Insider Threat Awareness Month September is designated National Insider Threat Awareness Month, according to a group of federal agencies and tech industry organizations.By Sandra Henry StockerSep 11, 20245 minsLinux
  • PODCASTS
  • VIDEOS
  • RESOURCES
  • EVENTS

NEWSLETTERS

Newsletter Promo Module Test

Description for newsletter promo module.

Managing and monitoring swap space on Linux (2024)

FAQs

How to manage swap space in Linux? ›

Managing Swap Space in Linux
  1. Create a swap space. To create a swap space, an administrator need to do three things: ...
  2. Assign the partition type. ...
  3. Format the device. ...
  4. Activate a swap space. ...
  5. Persistently activate swap space.
Jan 5, 2017

How do I monitor swap usage in Linux? ›

How you can view the amount of swap space on your Linux system? You can use the swapon –show command to view the swap space on your system. Another useful command is the free command that displays both swap space and memory usage. With -m, the results are displayed in MBs instead of KBs.

What is the recommended swap space size for Linux? ›

Swap space should be twice the size of RAM in case the RAM amount is below 2 GB. If RAM amounts to more than 2 GB, then swap space should be the size of RAM + 2 GB. For example, 6GB of swap for 4GB of RAM.

How to resolve high swap utilization in Linux? ›

An easy way to do this is to run 'free -m' to see what is being used in swap and in RAM. Once you power it off, you can wait an arbitrary amount of time (30 sec or so) to give the operation time to complete, then power the swap back on. This clears the swap memory cache and re-enables it.

How do I manage space in Linux? ›

Linux disk space commands like df, du, and ncdu are crucial for managing and monitoring disk usage. They help users optimize storage, pinpoint large files, and maintain efficient disk management. This article will delve into popular Linux disk commands and guide you on their effective usage.

What is swap space management? ›

This action of moving a process out from main memory to secondary memory is called Swap Out and the action of moving a process out from secondary memory to main memory is called Swap In. Swap-space management is a technique used by operating systems to optimize memory usage and improve system performance.

What happens if swap space is full in Linux? ›

If the swap space is full, the system starts swapping out active memory, leading to performance degradation and even system crashes. Information loss. If the system loses power during intensive swapping, the swap file is not flushed to disk, resulting in data loss.

How do I reduce swap file size in Linux? ›

If you want to change the size of your swap file (which is 1GB by default on Ploi) just follow the following steps.
  1. Turn off all running swap processes: swapoff -a.
  2. Resize swap fallocate -l 1G /swapfile (change 1G to the gigabyte size you want it to be)
  3. CHMOD swap: chmod 600 /swapfile.

How do I permanently increase swap space in Linux? ›

Enhance Linux Performance: Step-by-Step Guide to Increase Swap File Space
  1. What is swap space? ...
  2. When to increase swap space. ...
  3. Check current swap usage. ...
  4. Disable the swap file. ...
  5. Create a new larger swap file. ...
  6. Rename and replace the old swap file. ...
  7. Set permissions on the new swap file. ...
  8. Re-enable the new swap file.
Jan 5, 2024

Is 100% swap usage bad? ›

Re: Swap usage at almost 100% - What could be wrong??

That gives more flexibility for the OS and yourself and then you can start figuring out which task or process is maybe leaking memory or any other problem, but this is just normal behavior I would say.

How do I reduce swap space in RHEL? ›

How To Reduce An LVM Swap Partition On CentOS 7 /RHEL 7
  1. Step – Disable the LVM swapping Partition. ...
  2. Step Reduce the LVM logical volume. ...
  3. Step – Format the new Swap LVM logical volume. ...
  4. Step – Enable the Swap LVM logical volume. ...
  5. Step – Test and checking.
Jan 23, 2017

What is considered high swap usage? ›

By default, the alert is triggered when NG Firewall's swap partition is more than 25% full. For example, if your NG Firewall has 4 GB of swap, the alert will trigger when that partition is larger than 1 GB.

How do I allocate swap space? ›

To create and enable SWAP on Linux, we follow these 3 steps.
  1. Step 1: SSH into your Linux system. ...
  2. Step 2: Check Swap. ...
  3. Step 3: Check the free disk space. ...
  4. Step 4: Create the Swap file. ...
  5. Step 5: Create Swap Partition and enable Swap on Linux.
Dec 20, 2020

How to set swap partition in Linux? ›

To create and use a swap partition:
  1. Use fdisk to create a disk partition of type 82 ( Linux swap ) or parted to create a disk partition of type linux-swap of the size that you require.
  2. Initialize the partition (for example, /dev/sda2 ) as a swap partition: # mkswap /dev/sda2.

Top Articles
Top Highly Volatile Stocks in India for Intraday Trading
Difference between GPS and Satellite Navigation System
Craigslist Home Health Care Jobs
How To Fix Epson Printer Error Code 0x9e
Pet For Sale Craigslist
Elleypoint
Erika Kullberg Wikipedia
Richard Sambade Obituary
Gunshots, panic and then fury - BBC correspondent's account of Trump shooting
Palace Pizza Joplin
Prices Way Too High Crossword Clue
R Tiktoksweets
Unit 1 Lesson 5 Practice Problems Answer Key
Miami Valley Hospital Central Scheduling
Conduent Connect Feps Login
Edible Arrangements Keller
Amelia Bissoon Wedding
What to do if your rotary tiller won't start – Oleomac
Marion County Wv Tax Maps
Craigslist Motorcycles Orange County Ca
Bowlero (BOWL) Earnings Date and Reports 2024
Les Rainwater Auto Sales
De beste uitvaartdiensten die goede rituele diensten aanbieden voor de laatste rituelen
623-250-6295
Amih Stocktwits
Craigslist Roseburg Oregon Free Stuff
Marquette Gas Prices
Belledelphine Telegram
Craigslist Comes Clean: No More 'Adult Services,' Ever
Srjc.book Store
Homewatch Caregivers Salary
Spy School Secrets - Canada's History
Plato's Closet Mansfield Ohio
Tyler Sis 360 Boonville Mo
October 31St Weather
Msnl Seeds
The Transformation Of Vanessa Ray From Childhood To Blue Bloods - Looper
Craigslist Pa Altoona
Thelemagick Library - The New Comment to Liber AL vel Legis
Dogs Craiglist
How to Quickly Detect GI Stasis in Rabbits (and what to do about it) | The Bunny Lady
Craigs List Hartford
11 Best Hotels in Cologne (Köln), Germany in 2024 - My Germany Vacation
Lucifer Morningstar Wiki
Citymd West 146Th Urgent Care - Nyc Photos
Hanco*ck County Ms Busted Newspaper
Food and Water Safety During Power Outages and Floods
Germany’s intensely private and immensely wealthy Reimann family
Google Flights Missoula
Sam's Club Fountain Valley Gas Prices
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 6063

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.