Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

Linux Swap Memory: When and How to Use It

Published

Linux and Commands

When you use Linux, you might come across a term called “swap space.” It’s a part of your hard drive or SSD set aside to help your system handle more stuff when your RAM is full. This article will explain what swap space is, when you need it, and how to use it.

What Is Swap Space?

Swap space is an area on your disk that acts like extra memory. If your physical RAM (the fast working memory of your computer) is all used up, your system can move some of the less-used data from RAM to the swap space. This process lets your computer keep working instead of freezing or crashing, although it might get slower because disks are slower than RAM.

Do You Need Swap Space?

In the past, swap space was almost always needed because computers had much less RAM. Today, many computers have lots of RAM (often 8GB, 16GB, or more), but swap space can still be helpful. Here’s when you might need it:

  • Your system runs out of RAM often because you use memory-heavy apps or many apps at once.
  • You want your computer to avoid crashing when it runs out of memory.
  • Your system needs to support “hibernation” (saving everything in RAM to disk when you power off).
  • You use a server that might face unpredictable workloads.

If you have a lot of RAM and only do basic tasks, you might not notice if you don’t have swap space. If your system starts to slow down or act oddly when you open a lot of programs, adding or adjusting swap can help.

Types of Swap Space

There are two main ways to set up swap on Linux:

  • Swap Partition: This is a separate part of your hard drive just for swap. It’s usually set up when you install Linux.
  • Swap File: This is a special file on your existing filesystem that acts as swap space. It’s more flexible because you can resize or remove it without messing with partitions.

How To Check If You Have Swap

You can see if your system has swap space and how much is being used.

Step 1: Open a terminal window.

Step 2: Type swapon --show and press Enter. You will see a list of swap partitions or files in use. If nothing is listed, you don’t have active swap.

Step 3: To see more details, type free -h to show total, used, and free swap space along with your RAM.

How Much Swap Space Should You Have?

The amount of swap space depends on your needs. There is no strict rule, but here are some general guidelines:

  • If you have less than 2GB RAM, use swap equal to 2 or 3 times your RAM.
  • If you have 2GB-8GB RAM, use swap that’s about the same size as your RAM.
  • If you have more than 8GB RAM, having swap that is half or even less than your RAM is usually fine unless you use hibernation or do memory-heavy tasks.

If you want to use hibernation, your swap size should be equal to or larger than your RAM size.

How To Add Swap Space Using a Swap File

Adding swap with a swap file is common and easy. Here is how you can do it:

Step 1: Decide how big you want your swap file to be. For example, for 2GB, use 2G.

Step 2: Create the swap file. Type: sudo fallocate -l 2G /swapfile

Step 3: Secure the file by setting the right permissions: sudo chmod 600 /swapfile

Step 4: Format the file for swap: sudo mkswap /swapfile

Step 5: Enable the swap file: sudo swapon /swapfile

Step 6: To make the change permanent, add this line to your /etc/fstab file: /swapfile none swap sw 0 0

How To Turn Off Or Remove Swap

If you need to turn off swap, maybe for troubleshooting or because you want to resize it, you can do this:

Step 1: Turn swap off with: sudo swapoff -a

Step 2: If you want to remove a swap file, delete the line in /etc/fstab that lists your swap file, and then delete the actual file with sudo rm /swapfile

Swapiness: Controlling Swap Usage

Linux controls how often it uses swap through a setting called swappiness. The swappiness value is between 0 and 100. Lower values mean the system avoids using swap as much as possible. Higher values make Linux use swap more readily.

Step 1: Check your current swappiness with: cat /proc/sys/vm/swappiness

Step 2: To set a new value (for example, 10): sudo sysctl vm.swappiness=10

Step 3: To make the change permanent, add vm.swappiness=10 to your /etc/sysctl.conf file.

When Not To Use Swap

Swap is helpful, but there are times when you might want to avoid using it:

  • If you use an SSD, frequent writing to swap can wear it out faster. Modern SSDs are much better, but it’s still something to watch out for.
  • Heavy use of swap makes your system slow because your disk is much slower than your RAM.
  • If you are building a real-time system or a system needing maximum performance (like high-speed servers or gaming PCs), you might want to keep swap use to a minimum by adding more RAM instead.

Conclusion

Swap space is a helpful safety net for Linux systems, especially if you run out of RAM. Most desktop and server users can benefit from having some swap space, even if just for emergencies. If your system is struggling with low memory, adding a swap file only takes a few commands and can save you from crashes or slowdowns. Adjusting how Linux uses swap can help you get the right balance for your system’s needs.

Related guides

Account services are unavailable until the production backend is configured.