25% off on first invoice for all services*

SPRING SALE

Use coupon

*Offer valid for new customers only

25% off on first invoice for all services*

SPRING SALE

Use coupon

*Offer valid for new customers only

Need help?

Our experts have had an average response time of 11.43 minutes in March 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

Add Swap partition on an Ubuntu server – 2 different ways explained

by | Feb 26, 2019

It’s common that many services fail due to memory crunch on the server.

That’s where the role of swap space come into picture. And, it’s true that swap partition can improve the performance and stability of the system.

At Bobcares, we help server owners to add or expand swap partition as part of our Server Management Services.

Today, let’s discuss the 2 different ways to add swap partition on an Ubuntu server.

 

What is Swap and Why it’s needed?

Before we move on to the steps to add swap partition on Ubuntu, let’s first understand what is swap and the role of swap space.

Swap space is actually a virtual memory. In other words, swap space is like adding RAM, but not physical memory. It’s more useful for systems with low memory, but have resource intense applications.

If physical memory is full and the server needs more memory to run applications, it moves the inactive pages from memory to swap space. The system uses this free space for other active programs.

 

Add Swap partition on Ubuntu – How much is needed?

Ideally, our Server Experts always advise to keep the swap memory equal to the size of physical memory at minimum and double the size of the physical memory at maximum. For example, if the physical memory on the server is 16GB, swap space should be between 16 – 32 GB.

For Ubuntu systems with RAM less than 1GB, the swap should be atleast the size of RAM and atmost double the RAM. Similarly, for systems with more than 1GB RAM, we recommend swap space should be atleast equal to the square root of RAM size and atmost double the RAM.

Add Swap partition on Ubuntu – How we do it?

Now, let’s see how our Server Administrators add swap partition on an Ubuntu server.

Before, we start adding swap on the server, we first confirm whether swap partition is configured on the Ubuntu server. For that, we use the below command.

swapon --show

 

If the system doesn’t display any output, the swap partition on the system is not enabled. In addition to that, our Support Engineers verify the same using the free command.

 

add swap partition ubuntu

How to verify the Swap information?

 

We check the Swap section here. The 0 value shows that swap on the server is not enabled. The next step is to create swap space on the server.

We can either use a dedicated hard drive partition to add new swap space or create a swap file and use it as swap space. Let’s see both cases in detail.

 

1) Create a swap partition

If we have an extra hard disk or free space on an existing disk, we can go ahead and create a swap partition. But, be careful here. Any wrong step can result in major data loss. That’s why, we recommend to have a working backup of the data files before moving ahead.

Our Support Experts use the fdisk utility to create new partitions. For example, we use the following command to create a new partition from /dev/sda.

fdisk /dev/sda

 

Most importantly, we assign the partition value as 82, so that system detects it as a swap partition. Alternatively, customers can use tools such as GParted, cfdisk, etc. for partitioning and formatting.

Further, we convert the new partition as swap area using the below command.

mkswap /dev/sda1

 

And, enable the swap space using the below command.

swapon -s /dev/sda1

 

Finally, to make this swap space available even after reboot, we make some modifications in the /etc/fstab file.

cat /etc/fstab
/dev/sda1 none swap sw 0 0

 

Alternatively, in some systems we add the UUID(Universally Unique Identifier) for the swap partition in the /etc/fstab file.

UUID=615b3be3-719c-5e21-a944-a033114f3cf5 none swap sw 0 0

 

Our Support Engineers get the UUID information of a partition using the blkid command. We always prefer to use the UUID, because it remains constant even if we move the partition around or rename it.

Finally, we verify the new swap space using the below commands.

swapon -s
free -m

 

However, consider scenarios such as Ubuntu running on a virtual machine and there’s no swap partition or you have no additional space on the server. Then, the only option is to create a swap file and use it as swap space on the server.

 

2) Create a Swap file

Let’s now see the steps to add swap partition on Ubuntu using swap file. This involves a series of steps. Let’s discuss them in detail.

a) Select the partition

Firstly our Support Engineers check the current disk usage on the system using the below command.

df -h

 

The output will be like this.

 

add swap partition ubuntu

Output of df -h command

 

This gives us an idea of the disk usage on each partition and the total free space. Based on this, we select a partition that can hold the swap file.

 

b) Create a swap file

Once we’ve confirmed the available disk space, we create a swap file within the selected filesystem. Our Support Engineers usually use fallocate utility to create swap file.

For example, if we need to create a swap file named “swapfile” of size 1GB, we use the following command.

fallocate -l 1G /swapfile

 

However, fallocate utility may not be installed on some systems. In such cases, we use dd command to create a swap file.

dd if=/dev/zero of=/swapfile bs=1M count=1024

 

Further, we verify if correct space is allocated to the /swapfile using the below command.

ls -lh /swapfile

 

Most importantly, we ensure that there are strict permissions for this file, so that only users with root privileges can read the contents. That is, we change the permission of swapfile to 600.

chmod 600 /swapfile

 

c) Enable the swap file

Further, we use the mkswap utility to convert the /swapfile as Linux swap space.

mkswap /swapfile

 

add swap partition ubuntu

How to convert swap file as swap space?

 

Later, we activate this swap file so the system can now use it.

swapon /swapfile

 

Once done, we check the swap status on the server using the below command.

#swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -1

 

In addition to that, we double check the same using the free command as well.

 

d) Make the swap permanent

Finally, our Server Experts make changes to the /etc/fstab file to make the swapfile available after every server reboot. For instance, we add the following entry in the /etc/fstab file to make the swap file permanent.

/swapfile none swap sw 0 0

 

You’re done, reboot the system to use the new swap space.

[Trouble adding swap space on your server? Click here and get one of our Support Experts to look into your issue.]

 

Add Swap partition on Ubuntu – Post checks to be done

Now we know how to add swap partition on Ubuntu. However, our Dedicated Engineers do certain tweaks to improve system performance while using swap memory.

1) Swappiness

Swappiness is a kernel parameter that defines how often the kernel moves processes from physical memory to swap space. It can have values ranging from 0 to 100. And, the default value in Ubuntu is 60. The higher the value, the more often the system uses swap. We can get the current swappiness value of the system from the /proc/sys/vm/swappiness file.

Our Support Engineers define the swappiness value after analyzing the type of applications, memory usage of the applications, server work load, etc. For instance, to set the swappiness value permanently to 20, we add the following code in /etc/sysctl.conf file.

vm.swappiness=10

2) Cache Pressure settings

Another kernel parameter that affects swap performance is  VFS Cache Pressure settings. This controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects. We can get the current value of this parameter from /proc/sys/vm/vfs_cache_pressure.

Our Support Engineers adjust the vfs_cache_pressure to an optimum value without affecting the system performance. For instance, we change this value to 50 by adding the following code in the file /etc/sysctl.conf.

vm.vfs_cache_pressure=50

 

[Need help in tweaking the kernel parameters to improve the system performance? Our Experienced Server Administrators can help your here. We are available 24/7.]

Conclusion

In short, adding swap space on the server helps server owners resolve out of memory problems to a great extent. However, it has a downside as well. Since swap space resides in hard disk, the access time of swap is slower. Today, we’ve discussed how our Server Experts add swap partition on Ubuntu servers and tweak it’s parameters to improve system performance.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF