How To Add Swap Space on Ubuntu 22.04
Managing resources efficiently is vital for maintaining the performance and stability of the OS. In this article, the methods of adding swap space to Ubuntu 22.04 is outlined to help users boost their platform's capacity to carry on memory-intensive activities.
Swap space acts as a virtual extension of physical memory (RAM), allowing the system to offload inactive processes when it is fully utilised. While Ubuntu 22.04 is highly efficient in memory management, adding or increasing paging area can be a practical solution for environments with small data storage unit or when running resource-heavy applications. This article provides a step-by step approach in creation, configuration, and optimisation of swap space, ensuring a smooth and efficient setup tailored to everyone's needs.
Prerequisites Copy link
Before adding swap space on Ubuntu 22.04, make sure the following prerequisites are satisfied to avoid potential issues:
-
Administrative Privileges: User must have
rootorsudoaccess to the platform to execute commands for creating and configuring swap space. -
Existing Disk Volume: Confirm that the instance has sufficient free disk storage to allocate for the desired swap size. Deploy the following instruction to check disk space:
df -h
-
Current Status: Determine whether a swap space already exists and come up with the decision to expand it. Utilise the instruction below to verify:
sudo swapon --show-
Suitable Performance Needs Assessment: Determine the required capacity of the swap space according to the current storage resource and workload. A common rule is to have at least same amount as the RAM size, but this may vary depending on your use case.
And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS.
What is Swap Copy link
A crucial part of Linux memory management, swap space is intended to improve system performance and stability by increasing the system's accessible capacity beyond the physical random-access memory (RAM). The OS frees up memory for running processes by offloading idle or seldom used data to the paging space area when the RAM is completely utilised. This procedure enables the system to manage resource-intensive tasks more effectively and keeps apps from crashing because of memory shortages. Depending on the demands of the user, swap can be implemented in Ubuntu as a file or as a separate disc. This can be useful, but it cannot take the place of enough RAM. Because disc storage has slower read and write rates than physical memory, an over-reliance on this might result in performance loss. Optimising system performance requires an understanding of swap's operation and proper configuration, especially for tasks like managing apps on platforms with limited RAM, operating virtual machines, or compiling huge codebases.
Swap Advantages Copy link
Swap space is an important part of Linux environment memory management because it provides a number of benefits. The following advantages are offered by swap:
- Prevents System Crashes
- Supports Memory-Intensive Applications
- Enhances Multitasking
Smoother multitasking without sacrificing speed for platforms managing numerous processes at once by balancing memory use by offloading less important operations.
- Provides Flexibility
Swap space allows for the dynamic addition or resizing of paging space, which facilitates system requirements adaptation without requiring disc repartitioning.
- Extends Uptime Period
It is a short-term fix to increase stability and prolong its uptime under high loads in situations where replacing physical memory is not immediately practical.
- Facilitates Hibernation
Swap is crucial for systems set up to utilise the hibernate feature since it keeps the contents of the RAM in place when the system is turned off, enabling a smooth restart.
- Supports Low-Memory Systems
For lightweight systems, this is beneficial because it guarantees that critical operations continue to run even when memory is limited on devices with little physical memory.
Swap is essential for increasing overall system resilience and flexibility, especially in resource-constrained contexts, even while it cannot replace physical RAM and shouldn't be over-relied upon.
Swap Disadvantages Copy link
Although swap space has several benefits for memory management, there are a few significant drawbacks that should be taken into account when setting it up.
- Slower Performance Compared to RAM
- Increased Disk Wear
- Latency in Resource-Intensive Tasks
When the system relies heavily on swap, tasks that require high memory bandwidth, such as video editing or large-scale data analysis, may experience significant delays due to slower data transfer rates.
- Limited Effectiveness in Low-RAM Scenarios
While swap can extend memory, it is not a substitute for adequate RAM. On systems with extremely low physical memory, relying on swap may not be enough to handle modern applications efficiently.
- Hibernation Dependency
If the swap space is insufficient, hibernation may fail as it requires swap to store the contents of the RAM. Misconfigured swap sizes can lead to system errors during hibernation attempts.
- Additional Storage Allocation
Allocating swap space reduces the available storage for other purposes. For systems with limited disk capacity, dedicating a portion to swap may not be feasible.
-
Complexity in Configuration
Optimising swappiness and settings require careful planning and monitoring. Poor configuration may lead to either underutilisation or excessive reliance, both of which impact system performance.
How to Add Swap Space by Creating a Swap File Copy link
Making a swap file in Ubuntu 22.04 to increase swap space is a simple procedure that can assist boost system performance, particularly on systems with low RAM.
Here is a thorough, step-by-step guide to assist you with the process:
-
Make sure swap space is enabled before making a new file. Run the instruction below.
sudo swapon --show-
Based on the RAM capacity and usage needs, choose the swap file's size. A typical rule of thumb is:
-
- For systems with less than 2 GB of RAM, swap size is equal to RAM size × 2.
- For systems with more than 2 GB of RAM, swap size equals RAM size.
-
Choose the location of the file, which is often the
rootdirectory. Adjust to the user's preferred swap size. To do it, use thefallocatecommand.
sudo fallocate -l 4G /swapfile-
If
fallocateis unavailable or gives an error, employ theddcommand.
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
-
-
bs=1M: Sets the block size to 1 Megabyte. -
count=4096: Creates a 4GB file (4096 × 1MB).
-
-
Verify that the permissions are configured appropriately to prevent unauthorised access. Execute the following command.
sudo chmod 600 /swapfile-
It is necessary to format the file as swap space. After that, swap can be activated. Execute the command listed below.
sudo mkswap /swapfile
sudo swapon /swapfile-
To verify if it has been added, use the instructions listed below, appropriately.
sudo swapon --show
free -h
-
Add the swap file to the
/etc/fstabfile to guarantee it stays active following a reboot. Perform the following steps.
-
- Backup the
fstabfile before editing.
- Backup the
sudo cp /etc/fstab /etc/fstab.bak-
-
Add the swap record in
fstab.
-
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab-
-
Validate using command below.
-
cat /etc/fstab
Configuring Swappiness (Optional) Copy link
Swappiness controls the kernel's use of swap space. 60 is the default value. Usage rises with higher values and falls with lower values.
-
Verify current swappiness value by running command below.
cat /proc/sys/vm/swappiness
-
Use the
sysctlutility to temporarily modify the swappiness. The value is lowered to 40 from 60 by the subsequent command.
sudo sysctl vm.swappiness=40
-
To make the changes permanent, run these commands respectively.
echo 'vm.swappiness=40' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
Modify Cache Pressure (Optional) Copy link
Cache pressure regulates the kernel's propensity to recover caching memory, which can be lessened with lower values.
-
If for example, a user wants to set VFS Cache Pressure to 40, this can be set using the commands below respectively.
echo 'vm.vfs_cache_pressure=40' | sudo tee -a /etc/sysctl.confsudo sysctl -p-
Verify that the swap file is operational and set up properly. Use the commands below to check it.
sudo swapon --showfree -hIncreasing Swap Space with Swap File Copy link
To resize the system's swap file, use the following actions.
-
Temporarily disable the swap file.
sudo swapoff /swapfile-
Change the size of the swap file to the preferred size. Replace 8G with your desired new size.
-
-
Using the
fallocatecommand
-
sudo fallocate -l 8G /swapfile-
Using the
ddcommand
sudo dd if=/dev/zero of=/swapfile bs=1M count=8192-
To adjust for the new size, reinitialise the swap file.
sudo mkswap /swapfile-
Activate the swap file that has been resized.
sudo swapon /swapfile-
Validate that the swap space has been updated from 4GB to 8GB.
sudo swapon --showfree -hConclusion Copy link
To sum up, creating a swap file in Ubuntu is a simple procedure that can greatly improve system speed, especially when working with memory-demanding apps or when physical RAM is at limited availability. Without the need for intricate partitioning, users can rapidly increase the virtual memory of their system by following the instructions to create, format, and activate a swap file. The swap space will also be active across reboots if the swap file is made permanent via the /etc/fstab file. The memory management can be further optimised by modifying variables like swappiness. All things considered, making a swap file is a practical and adaptable way to enhance Ubuntu system efficiency and stability.
You can install Ubuntu on a VPS on Hostman.