Swap on Ubuntu is a feature that allows the operating system to use a portion of the hard drive as virtual memory when the physical memory (RAM) is complete. This helps prevent system crashes and slowdowns by temporarily allowing the operating system to store data that would otherwise be stored in RAM. This article will provide an overview of Swap on Ubuntu, How to create it. Its features, use cases, advantages, and disadvantages. We will also compare Swap on Ubuntu to other popular operating systems to provide a better understanding of its capabilities.
What is Swap on Ubuntu?
Swap on Ubuntu is a feature that allows the operating system to use a portion of the hard drive as virtual memory when the physical memory (RAM) is complete. This virtual memory, called swap space, allows the operating system to store data that would otherwise be stored in RAM temporarily. When the physical memory is complete, the operating system moves the least frequently used data from RAM to swap space, freeing up physical memory for other processes.
Check the System for Swap Information
Before we begin, we will look at our operating system to see if we already have some swap space. We can have multiple swap files or partitions, but one should generally be enough.
We can see if the system has any configured swap by typing:
sudo swapon -s
Filename Type Size Used Priority
If you only get back the table’s header, as shown above, you do not currently have any swap space enabled.
Another, more familiar way of checking for swap space is with the free utility, which shows us system memory usage. We can see our current memory and swap usage in Megabytes by typing:
free -m total used free shared buffers cached Mem: 3953 154 3799 0 8 83 -/+ buffers/cache: 62 3890 Swap: 0 0 0
As you can see above, our total swap space in the system is “0”. This matches what we saw with the previous command.
Check Available Space on the Hard Drive Partition
The typical way of allocating space for swap is to use a separate partition devoted to the task. However, altering the partitioning scheme is not always possible. We can just as easily create a swap file on an existing partition.
Before we do this, we should be aware of our current disk usage. We can get this information by typing:
df -h Filesystem Size Used Avail Use% Mounted on /dev/vda 59G 1.3G 55G 3% / none 4.0K 0 4.0K 0% /sys/fs/cgroup udev 2.0G 12K 2.0G 1% /dev tmpfs 396M 312K 396M 1% /run none 5.0M 0 5.0M 0% /run/lock none 2.0G 0 2.0G 0% /run/shm none 100M 0 100M 0% /run/user
As you can see on the first line, our hard drive partition has 55 Gigabytes available, so we have much space to work with. However, this is on a fresh, medium-sized VPS instance, so your actual usage might be very different.
Although there are many opinions about the appropriate swap space size, it depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point.
Since my system has 4 Gigabytes of RAM and doubling that would take a significant chunk of my disk space that I’m not willing to part with, I will create a swap space of 4 Gigabytes to match my system’s RAM.
Create a Swap File
Now that we know our available hard drive space, we can create a swap file within our filesystem.
We will create a swapfile file in our root (/) directory. The file must allocate the space we want for our swap file. There are two main ways of doing this:
The Traditional, Slow Way
Traditionally, we created a file with preallocated space using the dd command. This versatile disk utility writes from one location to another location.
We can use this to write zeros to the file from a particular device in Linux systems located at /dev/zero that spits out as many zeros as requested.
We specify the file size by combining bs for block size and count for the number of blocks. What we assign to each parameter is almost entirely arbitrary. What matters is what the product of multiplying them turns out to be.
For instance, we want to create a 4 Gigabyte file in our example. We can do this by specifying a block size of 1 Gigabyte and a count of 4:
sudo dd if=/dev/zero of=/swapfile bs=1G count=4 4+0 records in 4+0 records out 4294967296 bytes (4.3 GB) copied, 18.6227 s, 231 MB/s
Check your command before pressing ENTER because this has the potential to destroy data if you point the (which stands for output file) to the wrong location.
We can see that 4 Gigabytes have been allocated by typing:
ls -lh /swapfile -rw-r–r– 1 root root 4.0G Apr 28 17:15 /swapfile
If you’ve completed the command above, you may notice that it took quite a while. You can see in the output that it took my system 18 seconds to create the file. It has to write 4 Gigabytes of zeros to the disk.
If you want to learn how to create the file faster, remove the file and follow along below:
sudo rm /swapfile
The Faster Way
The quicker to get the same file is by using the fallocate program. This command instantly creates a file of a preallocated size without having to write dummy content.
We can create a 4 Gigabyte file by typing:
sudo fallocate -l 4G /swapfile
The prompt will be returned to you almost immediately. We can verify that the correct amount of space was reserved by typing:
ls -lh /swapfile
-rw-r–r– 1 root root 4.0G Apr 28 17:19 /swapfile
As you can see, our file is created with the correct amount of space set aside.
Enabling the Swap File
Right now, our file is created, but our system does not know that this is supposed to be used for swap. We need to tell our system to format this file as a swap and enable it.
Before we do that, though, we need to adjust the permissions on our file so that it isn’t readable by anyone besides the root. Allowing other users to read or write this file would be a security risk. We can lock down the permissions by typing:
sudo chmod 600 /swapfile
Verify that the file has the correct permissions by typing:
ls -lh /swapfile
-rw——- 1 root root 4.0G Apr 28 17:19 /swapfile
As you can see, only the columns for the root user have the read and write flags enabled.
Now that our file is more secure, we can tell our system to set up the swap space by typing:
sudo mkswap /swapfile Setting up swapspace version 1, size = 4194300 KiB no label, UUID=e2f1e9cf-c0a9-4ed4-b8ab-714b8a7d6944
Our file is now ready to be used as a swap space. We can enable this by typing:
sudo swapon /swapfile
We can verify that the procedure was successful by checking whether our system reports swap space now:
sudo swapon -s
Filename Type Size Used Priority /swapfile file 4194300 0 -1
We have a new swap file here. We can use the free utility again to corroborate our findings:
total used free shared buffers cached Mem: 3953 101 3851 0 5 30 -/+ buffers/cache: 66 3887 Swap: 4095 0 4095
Our swap has been set up successfully, and our operating system will begin to use it as necessary.
Make the Swap File Permanent
We have our swap file enabled, but when we reboot, the server will not automatically allow the file. We can change that, though, by modifying the fstab file.
Edit the file with root privileges in your text editor:
sudo nano /etc/fstab
At the bottom of the file, you need to add a line that will tell the operating system to use the file you created automatically:
/swapfile none swap sw 0 0
Save and close the file when you are finished.
Tweak your Swap Settings
You can configure a few options that will impact your system’s performance when dealing with swaps.
The swappiness parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage.
With values close to zero, the kernel will not swap data to the disk unless necessary. Remember, interactions with the swap file are “expensive” because they take a lot longer than interactions with RAM, and they can cause a significant reduction in performance. Telling the system not to rely on the swap much will generally make your system faster.
Values closer to 100 will try to put more data into a swap to keep more RAM space free. Depending on your applications’ memory profile or what you use your server for, this might be better sometimes.
We can see the current swappiness value by typing:
cat /proc/sys/vm/swappiness 60
For a Desktop, a swappiness setting of 60 is not a wrong value. For a VPS system, we’d probably want to move it closer to 0.
We can set the swappiness to a different value by using the sysctl command.
For instance, to set the swappiness to 10, we could type:
sudo sysctl vm.swappiness=10 vm.swappiness = 10
This setting will persist until the next reboot. We can fix this value automatically at restart by adding the line to our /etc/sysctl.conf file:
sudo nano /etc/sysctl.conf
At the bottom, you can add the following:
Save and close the file when you are finished.
Another related value that you might want to modify is the vfs_cache_pressure. This setting configures how much the system will cache inode and dentry information over other data.
This is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it’s an excellent thing for your system to cache. You can see the current value by querying the proc filesystem again:
cat /proc/sys/vm/vfs_cache_pressure 100
As it is currently configured, our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:
sudo sysctl vm.vfs_cache_pressure=50 vm.vfs_cache_pressure = 50
Again, this is only valid for our current session. We can change that by adding it to our configuration file as we did with our swappiness setting:
sudo nano /etc/sysctl.conf
At the bottom, add the line that specifies your new value:
vm.vfs_cache_pressure = 50
Save and close the file when you are finished.
Swap Use Cases
Swap on Ubuntu is a crucial feature that can help prevent system crashes and slowdowns. Some of the use cases of Swap on Ubuntu are as follows:
- Preventing System Crashes – When the physical memory is complete, the operating system may crash or become unresponsive. Swap on Ubuntu prevents this by allowing the operating system to store data in swap space when the physical memory is full.
- Allowing Large Applications to Run – Large applications such as video editing software and virtual machines require significant memory. Swap on Ubuntu allows these applications to run by providing additional virtual memory when the physical memory is full.
- Enabling Hibernation – Hibernation is a feature that allows the operating system to save the system’s current state to disk and power off the computer. Swap on Ubuntu is required for hibernation to work correctly, providing the virtual memory needed to store the system’s current state.
Swap on Ubuntu has many features that make it essential for any Ubuntu installation. Some of the most notable features of Swap on Ubuntu are as follows:
- Dynamic Allocation – Swap on Ubuntu dynamically allocates swap space based on the current system workload. This ensures that the system always has enough virtual memory to operate correctly.
- Configurable Size – Swap on Ubuntu allows users to configure the swap space size. This can be useful for systems with limited storage capacity, enabling users to allocate only the necessary swap space.
- Encrypted Swap – Swap on Ubuntu supports encrypted swap space, providing an additional security layer for sensitive data.
- Swappiness – Swappiness is a parameter that controls how often the operating system swaps data between physical memory and swap space. Swap on Ubuntu allows users to configure the swappiness parameter and fine-tune system performance.
Advantages of Swap on Ubuntu
- Prevents System Crashes – Swap on Ubuntu prevents system crashes and slowdowns by providing additional virtual memory when the physical memory is full.
- Enables Large Applications to Run – Swap on Ubuntu allows large applications to run by providing additional virtual memory when the physical memory is full.
- Required for Hibernation – Swap on Ubuntu is required for hibernation to work correctly, as it provides the virtual memory needed to store the system’s current state.
- Dynamic Allocation – Swap on Ubuntu dynamically allocates swap space based on the current system workload, ensuring the system always has enough virtual memory to operate correctly.
Disadvantages of Swap on Ubuntu
- Slower than Physical Memory – Swap on Ubuntu is slower than physical memory, which can cause slowdowns when the operating system needs to swap data between physical memory and swap space.
- Limited Storage Capacity – Swap on Ubuntu uses a portion of the hard drive as swap space, which can limit the storage capacity available for other applications and data.
- May Cause Wear on Hard Drive – Swap on Ubuntu may cause wear on the hard drive, as it involves frequent read and write operations.
- Limited Support for Non-Ubuntu Systems – While swap space is a standard feature on most operating systems, the configuration and management of swap space may differ across different systems. This can create it challenging for users to switch between other operating systems.
Swap on Ubuntu is a crucial feature that allows the operating system to use a portion of the hard drive as virtual memory when the physical memory is full. This helps prevent system crashes, and slowdowns, enable large applications to run and is required for hibernation to work correctly.
Swap on Ubuntu has various features, including dynamic allocation, configurable size, encrypted exchange, and swappiness, making it a versatile element for any Ubuntu installation.
While Swap on Ubuntu has several advantages, such as preventing system crashes and enabling large applications to run, it also has disadvantages. Swap on Ubuntu is slower than physical memory, can cause wear on the hard drive, and may limit the storage capacity available for other applications and data.
Overall, Swap on Ubuntu is a vital feature that provides additional virtual memory when the physical memory is full. It is an essential feature for any Ubuntu installation and helps prevent system crashes and slowdowns. While Swap on Ubuntu may have some limitations and drawbacks, its benefits far outweigh its disadvantages, making it a must-have feature for any Ubuntu system.
Tips and tricks
In this tutorial, you have successfully learned how to add Swap in Ubuntu 14.04. If you want to learn more about SWAP, checkout these documents: Linuxize
If you want a server with Ubuntu 14.04 operating system, get started now with VPSie.
This tutorial is to help you based on this original tutorial On DigitalOcean; please use our tutorial for notes and tips.
Swap on Ubuntu is a feature that allows the operating system to use a portion of the hard drive as virtual memory when the physical memory is full. This helps prevent system crashes and slowdowns.
When the physical memory is full, Swap on Ubuntu allocates a portion of the hard drive as virtual memory. This virtual memory stores data that is not actively used, freeing up physical memory for other applications.
The amount of swap space required on Ubuntu depends on several cause , such as the amount of physical memory installed on your system and the type of applications you use. A general rule of thumb is to allocate swap space equal to or slightly larger than your system’s physical memory.
Yes, you can turn off Swap on Ubuntu. However, it is not recommended as it can cause system crashes and slowdowns if the physical memory is full.
You can check your Swap space usage on Ubuntu using the “free” or “swapon” command in the terminal. These commands will display the amount of swap space currently in use and the amount of swap space available.