Swap Memory: What It Is, How It Works, and How to Manage It

August 30, 2023

Introduction

Swap memory, also known as swap space, is a section of a computer's hard disk or SSD that the operating system (OS) uses to store inactive data from Random Access Memory (RAM). This allows the OS to run even when RAM is full, preventing system slowdowns or crashes.

In this article, you will learn what swap memory is, how it works, and how much swap memory you need.

Swap Memory: What It Is & How It Works

What is Swap Memory?

Swap memory, often referred to as swap space, is an extension of a computer's physical RAM residing on the hard drive or Solid State Drive (SSD). When the OS exhausts its available RAM, it swaps data between RAM and the swap space. This mechanism, known as swapping, enhances memory management efficiency.

Swap memory is important for systems with restricted RAM or those executing memory-intensive tasks. Without swap memory, these systems are susceptible to crashing when RAM capacity is exceeded.

Simplified diagram of the swapping process

Types of Swap Memory

There are two types of swap memory:

Both types serve the same purpose — providing virtual memory space on a computer's storage device and acting as an extension of a computer's RAM. 

Key differences between a swap partition and swap file are:

DifferencesSwap FIlesSwap Partitions
PermanenceTemporary or permanent.Permanent.
LocationAnywhere on the hard drive.In a dedicated section of a hard drive.
Disk spaceFragmented. However, contiguous when needed.Contiguous space.
SizeResizeable as needed.Fixed size.
ManageabilityCreated and deleted using file management tools.
More difficult to manage when located on a large or fragmented hard drive.
Easier to manage than a swap file.
Can be created and deleted using the operating system's disk management tools.
FlexibilityMore flexible.
Ability to be added or removed without altering the disk structure.
Less flexible due to the fixed partition size.
SecurityStored in the file system, which is more accessible to unauthorized users and less secure.More secure, as the data is less likely to be accessed by unauthorized users.
Use casesDesktops, laptops, and cloud environments.In servers and high-performance systems.

How Does Swap Memory Work?

Swap memory facilitates memory management by temporarily storing inactive data on the disk. This mechanism ensures uninterrupted system performance even when RAM is fully utilized.

Swap memory is used in specific memory management techniques:

  • Swapping. The operating system uses algorithms based on usage patterns to decide which data to move to/from swap space.
  • PagingThe OS transfers active data from saturated RAM to the swap space.
  • Reclaiming. The OS transfers data that hasn't been referenced recently from saturated RAM to the swap space.

The swapping process follows these steps:

1. The exhaustion of RAM capacity.

2. The identification of data that hasn't been accessed recently.

3. Transfer of data from RAM to a swap file or a dedicated swap memory partition.

4. Loading of new data or data from swap memory back into RAM.

Swap memory in the swapping/paging workflow

Note: The frequency and assertiveness of the data swapping process is controlled by a parameter called swappiness

Benefits of Using Swap Memory

 Swap memory is helpful in many cases. The benefits of using swap memory are:

  • RAM optimization. By relocating inactive data to the disk, swap memory reduces RAM consumption. This optimization allows the OS to support numerous programs and applications simultaneously.
  • Increased memory availability. Storing inactive data on disk allows for increased memory availability, which is particularly valuable for systems with limited RAM.
  • Enhanced system performance. Concurrent execution of multiple programs and applications is facilitated, leading to improved overall performance.
  • Continuous operation. Even in scenarios of full RAM usage, swap memory ensures continuous system operation.
  • Memory overcommitment. Enables systems to allocate more memory to processes than is physically available.
  • Process Isolation. Swap memory isolates processes, which helps to prevent conflicts and errors.

Challenges of Using Swap Memory

Although swap memory is valuable for systems with limited RAM, system performance degradation is possible. The downsides of using swap memory are:

  • Performance. Swapping data between RAM and disk is slower than accessing data directly from physical memory. This leads to performance degradation if excessive swapping occurs. The disk speed, the swap file size, and the amount of data being swapped contribute to the performance issues.
  • I/O bottleneck. Heavy swapping causes increased disk I/O, which sometimes becomes a bottleneck. The disk has to read and write data to the swap file, which takes time.
  • Fragmentation. Over time, swap space becomes fragmented, affecting swapping efficiency and leading to suboptimal memory usage. The reason is the OS is unable to write data to the swap file in contiguous blocks.
  • Reliability. Swap files reside on less reliable storage media compared to system memory. Disk failure corrupts or loses the swap file, causing data loss.
  • Capacity. The system's disk space limits the available swap space. 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.

Note: Avoid adjusting swappiness values before establishing a data loss prevention (DLP) strategy.

How to Check Swap Memory

Depending on the operating system, there are several commands to check swap memory. The text below covers the most common OSes.

Linux

To check swap memory on Linux, execute free, swapon, or top, or access and modify /proc/swaps file. Follow the examples below for each.

Checking Swap Memory on Linux Using the free Command

To check total allocated swap memory, run the following command:

free -m
linux free -m terminal output

The output displays the amount of swap memory available and the amount and percentage being used. The -m argument tells the command to display the output in megabytes.

Checking Swap Memory on Linux Using the swapon Command

To list all swap files and partitions, run the following command:

swapon --show
linux swapon --show terminal output

This command displays information about the swap files and partitions currently being used.

Checking Swap Memory on Linux Using cat /proc/swaps

Use cat to access the /proc/swaps file to print the list of all swap spaces configured on the system:

cat /proc/swaps
linux cat /proc/swaps terminal output

Checking Swap Memory on Linux or macOS Using top

The top command can be used to check swap memory size and usage in Linux and macOS:

top
linux top command output

This command shows all running processes, including memory usage. 

Press Ctrl+M to sort the processes by memory usage, showing which processes use the most swap memory.

Windows

In Windows, systeminfo prints various information about the system, including the amount of swap memory available and being used.

Enter the command:

systeminfo
systeminfo cmd output

macOS

As mentioned above, you can use the top command to check swap memory in macOS

top
macOS top terminal output

This command works the same way on macOS as it does on Linux.

Another way is using the Activity Monitor. Open the app and navigate to the System Memory tab.

The Swap Used value shows how much swap memory is currently being used.

macOS activity monitor

How Much Swap Memory Do You Need?

There is no one-size-fits-all answer to this topic, as the ideal amount of swap memory varies. However, having at least as much swap memory as the physical RAM size is the recommended minimum. For example, with 8GB of RAM, at least 8GB of swap memory is optimal.

It used to be a common practice to have at least twice as much swap memory as RAM. However, this is no longer necessary for modern systems as they are more efficient at managing memory and require less swap space.

In addition to the amount of RAM, there are other factors to consider when determining how much swap memory is needed:

  • The operating system. Different operating systems have different memory management strategies. For example, Linux systems rely more on swap space than Windows systems.
  • The types of programs. Memory-intensive programs, like video editing or gaming, require more swap memory than less demanding programs.
  • Hibernation. Hibernation allows users to save the computer state to disk and resume the processes later. Users need enough swap space to accommodate the saved state when using hibernation.

The best way to determine the right amount of swap memory is to start with the recommended amount. If any issue shows up, increase the amount of swap memory.

How to Increase/Decrease Swap Memory

Resizing swap memory is essential as the system workload changes over time. Therefore, the amount of swap memory the system needs varies.

Note: sudo or root privileges are necessary for most commands in the following text.

Increase Swap Memory

Increasing the swap space is the way to go if the system is running memory-intensive tasks. In the text below, learn how to expand swap memory for Linux, Windows, and macOS.

Linux

Follow these steps to increase swap memory in Linux:

1. Check current swap usage:

swapon --show
example of using swapon --show in linux terminal to check swap memory size

The output shows 2G.

2. Disable swapping with:

sudo swapoff -a

The command ensures the swap file is not in use but produces no output.

3. Change the swipe file size with fallocate. The syntax is:

sudo fallocate -l [size]G /path-to-swapfile

Increase file size by adding a number instead of [size] and using the path to the swapfile. For example:

sudo fallocate -l 3G /swapfile

The command doesn’t produce any output.

4. Secure permissions with chmod:

sudo chmod 600 /swapfile

5. Format the file as swap:

sudo mkswap /swapfile
sudo mkswap swapfile terminal output

6. Enable swap:

sudo swapon /swapfile

7. Make the changes permanent by editing the /etc/fstab in a text editor. For instance, open the file in Vim with:

vim /etc/fstab
vim /etc/stab output

Next, replace the line below with your requirements.

<file system> <mount_point> <type> <options> <dump> <pass>

For instance:

./swapfile none swap sw 0 0
Changing the /etc/stab file

In this example:

  • ./swapfile is the path to your swap file.
  • none specifies that there is no specific mount point for the file.
  • swap determines the filesystem type to use.
  • sw option shows that this entry is related to a swap file, it is short for swap.
  • 0 in the dump field tells the system not to include the filesystem in backups.
  • 0 in the pass field means to skip filesystem checks.

Note: If it's your first time using Vim, check out our Vim commands cheat sheet.

8. Verify the change with:

swapon --show
swapon --show 3G size

The output shows 3G as the swap memory size.

Windows

To increase swap memory in Windows, follow these steps:

1. Right-click This PC and select Properties

windows this PC properties

2. Choose Advanced system settings from Related settings.

Advanced system settings in Windows

3. Click Advanced tab

4. Click Settings under the Performance section.

Advanced system performance settings in Windows

5. Choose the Advanced tab.

Advanced tab in performance options window in Windows

6. Click Change under Virtual Memory.

Change allocated virtual memory for paging in Windows

7. Uncheck Automatically manage paging file size for all drives and select the drive.

Allocated paging memory settings in Windows

8. Choose Custom size and set the new size:

Choose custom size for virtual memory for paging in Windows

9. Choose the size and follow the guidelines for minimum and maximum size.

10. Restart the computer for changes to take effect

macOS

On macOS, swap memory works differently than on Windows or Linux. Virtual memory in macOS involves the use of:

  • Memory Compression. Compresses inactive memory pages' contents, reducing the need for swapping to disk and improving overall system performance.
  • Swap Space. Serves as a last resort when other memory management techniques cannot handle memory demands. 

Directly increasing swap space is not typically recommended for regular users, as it leads to unintended consequences.

Decrease Swap Memory

If the system is not running many memory-intensive tasks, reduce the swap space to free up disk space. While in Linux and Windows, there are steps to scale down swap size, the process is not recommended.

Linux

Follow these steps to increase swap memory in Linux:

1. Check current swap usage:

swapon --show
swapon --show command 3G

The output shows 3G

2. Disable swap with:

sudo swapoff -a

The command doesn’t make any output. 

3. Resize swap memory with GParted or a similar app.

Note: Gparted is not always installed in a Linux system. Run sudo apt install gparted to install it.

For Gparted, run:

sudo gparted

The Gparted interface opens and allows users to resize or move any partition.

However, in some cases, swap memory is not presented as a separate partition in GParted. In the case of a dedicated partition, the partition would belong to a filesystem type of linux-swap.

linux gparted resize swap partition

Decrease the size of this partition using GParted.

4. Recreate the swap file with:

sudo mkswap /swapfile
Recreating swap file

5. Enable swap with:

sudo swapon /swapfile

The command shows no output.

6. Reboot the system:

sudo reboot

7. Test the change with:

swapon -show
using swapon --show in linux to check that a swap file has been decreased successfully

The output shows the swap memory size of 1G.

Windows

The process of reducing swap file size on Windows is similar to increasing it. Follow these steps:

1. Right-click This PC and choose Properties:

2. Scroll down to Related settings and choose Advanced system settings.

Advanced system settings

3. Select the Advanced tab:

Windows system properties advanced tab

4. Search for Settings under Performance.

5. Choose the Advanced tab.

Windows system performance options advanced tab virtual memory section

6. Click Change under Virtual Memory.

7. Make sure the Automatically manage paging file size for all drives is not checked, select the drive, choose Custom size, and set the new size.

8. Restart the computer.

macOS

Decreasing swap memory on a Mac is complex and involves resizing partitions, which is sometimes risky and requires expert consultation.

A better option is to optimize the utilization of swap files on a Mac, enhancing memory management and overall performance. For instance:

  • Manage resource-intensive applications.
  • Use Safari instead of browsers like Chrome or Firefox.
  • Close applications after using them.

As macOS dynamically manages swap space based on system requirements, manually changing swap size isn't recommended for most users.

Swap Memory FAQs

This section answers the most common questions about swap memory.

Is Swap Memory the Same Thing as Swap Space?

The swap memory and swap space refer to the same concept in the context of computer systems and operating systems. When used interchangeably, the terms describe the storage portion, which serves as an extension of physical memory when the system's RAM is fully occupied. Still, in specific contexts, the terms are different:

  • Swap memory. Refers to the swap space portion allocated for holding data that has been swapped out of RAM.
  • Swap space. A more broad term. Encompasses swap memory, as well as other areas of disk space that are not currently being used for swapping, and the concept of using the space to manage memory overflow.

Are Swap Memory and Virtual Memory the Same Thing?

No, swap memory and virtual memory are not the same, although the terms are related in the context of computer systems. Swap memory is a specific type of virtual memory.

Virtual memory is an abstract concept created by the operating system. However, swap memory refers to the portion of virtual memory used to store data not actively being used at the moment.

Is Swap Memory Necessary?

Swap memory is optional, but it is beneficial in many cases. It improves the system's performance by allowing the operating system to run programs that require more memory than is physically available. It also helps prevent the system from crashing if it runs out of RAM.

Therefore, while swap memory is not strictly necessary, it is a good idea to have it enabled, especially when running programs needing a lot of memory.

What Happens if You Have no Swap Memory/Disable Swapping?

With no swap memory or turned-off swapping, the system cannot run memory-intensive programs. Performance problems and even system crashes occur as a consequence.

In addition, disabling swapping makes the system more unstable. The OS doesn't have a way to store data not currently being used, which leads to data corruption and other problems.

Conclusion

After reading this article, you now know everything about swap memory, including what it is and how it works. By following the steps in the above sections, you can increase or decrease swap space.

Next, learn about the steps to check memory usage in Linux.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Create a Linux Swap File
June 1, 2023

Swap files are essential to Linux memory management. Swap enables performance optimization and managing resource-intensive tasks. Knowing how to create and manage swap files is...
Read more
Intel Optane Memory vs SSDs vs RAM
December 24, 2019

With so many choices available on the market, it's sometimes challenging to assemble the best hardware configuration for your business. We analyzed the Intel Optane memory Module and found...
Read more
How to Check Disk Space in Linux
May 8, 2024

Managing disk space on a Linux server is an important task.This tutorial shows how to display disk usage from a command line in Linux. It is important to know how much disk space you have used up, as well as...
Read more
How to Use the vmstat Command
March 18, 2021

The vmstat command is a Linux monitoring utility used to see various system statistics and to diagnose performance bottleneck issues. Learn how to use the command in this simple tutorial with examples...
Read more