Systems trying to run large, memory-intensive applications with limited RAM can run into serious issues. Operations might become very slow, and the application can sometimes freeze completely or crash with out-of-memory errors. One workaround for this issue is to create extra swap space on the hard drive. The application can use this extra memory supply when RAM is running low. This guide explains the concept of swap space, and demonstrates how to increase swap space on Ubuntu.
What is Swap?
To understand how swap space works, it is also important to distinguish between two types of computer memory. These are Random Access Memory (RAM) and the more permanent hard disk drive (HDD) or solid-state drive (SSD) storage. RAM memory is volatile and designed to temporarily hold data during program execution.
The RAM contents change quickly and require electricity to maintain. If power to the system is shut down, all data in RAM is lost. The storage drives are designed to permanently store data. If the system is rebooted, the data on the drive is still intact. Computer storage drives have a much larger capacity than RAM does, but RAM access is significantly faster.
Swap space is located on the system’s storage drive. A swap file is a pre-allocated file that is configured to hold system swap data. A swap file is easier to resize than a partition when more swap space is required. On a virtual machine, where partitions are not available, a swap file is the only available option.
Even though the swap file is located on the storage drive, the system can use this space as if it were part of RAM. Active program data can be temporarily stored in either place. This process is not transparent to the user, as it is not possible to tell where any given piece of data is being stored.
The system uses RAM preferentially, but switches to using the swap space as necessary. As working memory is used up, Ubuntu temporarily stores data or modules in the swap area. Many programs are optimized to keep frequently used information in RAM, while background or inactive data is stored in the swap file. Ubuntu provides the user-configurable swappiness
variable to indicate how frequently swap space should be used.
As of release 17.04, Ubuntu uses a swap file rather than a partition. However, assuming your Linode is setup with our normal configurations, it has a 512MB swap disk rather than a swap file.
Advantages and Disadvantages of Using Swap Space
Swap files provide several benefits, but they have a few drawbacks too. Here are some of the advantages of using swap space.
- It helps guard against out-of-memory errors, along with related program freezes and system crashes.
- It increases the amount of functional memory and makes it easier to run memory-intensive applications.
- It enables the use of the Ubuntu hibernation feature. For more information on this feature, consult the Ubuntu swap FAQ.
Here are the main disadvantages of configuring a swap file on Ubuntu.
- It can cause excessive disk wear on older SSD drives. This is because working memory is accessed far more frequently than the permanent storage drive. Newer SSD drives do not typically have this problem.
- Accessing swap space is slower than using RAM. The penalty depends on the system architecture. However, programs with strict performance requirements can suffer significant slowdowns.
Overall, it is usually better to treat swap space as a safety mechanism to avoid memory errors and crashes. If a system has to consistently access the swap file during regular operations, consider upgrading it with more RAM.
How to Increase Swap Space in Ubuntu
The examples that follow are from Ubuntu 22.04 LTS. However, the same steps can be used for Ubuntu 20.04 LTS and 18.04 LTS. On 16.04 LTS, it is necessary to use a swap partition to add swap space. These instructions are only valid for creating a swap file on Ubuntu and are not necessarily accurate on other Linux distributions.
It is important to configure a large enough swap file. However, very large files are typically not necessary. A system with 2GB of RAM or less should have a swap file that is twice as large as the RAM. For larger systems, 4GB should suffice. The optimal amount of swap space depends on the system and how it is used.
It is possible to create multiple swap files. However, it is better to configure one swap file, especially because it is possible to resize it later. You can also use a swap file along with a swap partition.
How to Create a Swap File on Ubuntu
Some systems automatically configure a swap file during installation. To determine whether the system already has a swap file, use the swapon --show
command. If any files are displayed, this means one or more swap files already exist. Proceed to the “How to Increase Existing Swap Space” section for information on how to resize a swap file.
sudo swapon --show
Confirm swap space has not already been allocated using the free
command. This command can be used on most Linux systems to verify the swap space size.
free -h
Ensure there is enough space on the hard drive to create the swap space. Use the df -h
command and locate the entry for the root directory, which is listed as /. The proposed swap file should fit comfortably within the available disk space with some room to spare. The following example indicates the hard drive has 64G of available space. This is more than adequate.
df -h
Allocate memory for the swap file using the fallocate command. This command is used to reserve a certain amount of disc space for a file in advance. The following command creates a 1G file named swapfile in the root directory. Always give a swap file a very obvious name to avoid confusion.
sudo fallocate -l 1G /swapfile
Change the file permissions for swapfile so only root can write to it. This prevents other users from accidentally deleting or overwriting the file.
sudo chmod 600 /swapfile
Use the mkswap command to designate the new file as a swap file. This means it can be used for volatile memory when RAM space runs low.
sudo mkswap /swapfile
The file swap has now been created, but it is still disabled. Activate it using the swapon command.
sudo swapon /swapfile
A Summary of Swap Space on Ubuntu
A swap file is used to avoid running out of RAM when using memory-intensive applications. Ubuntu uses swap space to store information that would ordinarily be held in RAM on the hard drive. This guards against freezes and crashes, but can negatively affect performance.
Most systems are designed to hold active memory in RAM under normal circumstances. However, the swappiness setting configures the system to use the swap space more or less preferentially. This guide provides detailed instructions on how to increase swap space on Ubuntu. The memory for a swap file can be allocated using the Ubuntu fallocate command. The file can then be converted into swap space using the mkswap command, and enabled with swapon.