Swap Memory: What It Is & How It Works (2024)

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 theOS 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 (1)

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.

Swap Memory: What It Is & How It Works (2)

Types of Swap Memory

There are two types of swap memory:

  • Swap partition. Temporary storage space used when physical memory becomes fully utilized.
  • Swap file. Physical disk storage used to expand the swap space of available memory.

Both types serve the same purpose — providingvirtual memory spaceon 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.
  • Paging.The 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: What It Is & How It Works (3)

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 numerousprogramsandapplicationssimultaneously.
  • 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 diskI/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 becomesfragmented, 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 indata 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, executefree, swapon, ortop, 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
Swap Memory: What It Is & How It Works (4)

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
Swap Memory: What It Is & How It Works (5)

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

Checking Swap Memory on Linux Using cat /proc/swaps

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

cat /proc/swaps
Swap Memory: What It Is & How It Works (6)

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

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

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

Enter the command:

systeminfo
Swap Memory: What It Is & How It Works (8)

macOS

As mentioned above, you can use the top command to check swap memory inmacOS.

top
Swap Memory: What It Is & How It Works (9)

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.

Swap Memory: What It Is & How It Works (10)

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
Swap Memory: What It Is & How It Works (11)

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. Securepermissions with chmod:

sudo chmod 600 /swapfile

5. Format the file as swap:

sudo mkswap /swapfile
Swap Memory: What It Is & How It Works (12)

6. Enable swap:

sudo swapon /swapfile

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

vim /etc/fstab
Swap Memory: What It Is & How It Works (13)

Next, replace the line below with your requirements.

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

For instance:

./swapfile none swap sw 0 0
Swap Memory: What It Is & How It Works (14)

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
Swap Memory: What It Is & How It Works (15)

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

Swap Memory: What It Is & How It Works (16)

2. Choose Advanced system settings from Related settings.

Swap Memory: What It Is & How It Works (17)

3. Click Advanced tab

4. Click Settings under the Performance section.

Swap Memory: What It Is & How It Works (18)

5. Choose the Advanced tab.

Swap Memory: What It Is & How It Works (19)

6. Click Change under Virtual Memory.

Swap Memory: What It Is & How It Works (20)

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

Swap Memory: What It Is & How It Works (21)

8. Choose Custom size and set the new size:

Swap Memory: What It Is & How It Works (22)

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
Swap Memory: What It Is & How It Works (23)

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.

Swap Memory: What It Is & How It Works (24)

Decrease the size of this partition using GParted.

4. Recreate the swap file with:

sudo mkswap /swapfile
Swap Memory: What It Is & How It Works (25)

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
Swap Memory: What It Is & How It Works (26)

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.

Swap Memory: What It Is & How It Works (27)

3. Select the Advanced tab:

Swap Memory: What It Is & How It Works (28)

4. Search for Settings under Performance.

5. Choose the Advanced tab.

Swap Memory: What It Is & How It Works (29)

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.

Swap Memory: What It Is & How It Works (2024)

References

Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 6605

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.