Add Swap Space/Partition on Ubuntu

shivangpatel | Jan. 21, 2023, 6:26 p.m. | Linux,

Swap space or memory are backup plans for RAM. Whenever your system RAM is out of memory on that time OS uses swap memory as an extended RAM in the system. That's a simple logic behind the swap memory in the Linux system.

During the OS installation, if you forgot to add a swap memory partition then don't worry. You can add it later too. You have to follow a few steps for it. You can find more about swap memory on SwapFaq | Ubuntu Community Help Wiki

Step 1: Swap Information Check-up

sudo swapon --show

Use the free utility to verify that there is no active swap: free -h

Step 2: Hard Drive Partition Availability Check-up

df -h

Step 3: Create a Swap File

sudo fallocate -l 64G /swapfile1

ls -lh /swapfile1

Step 4: Enable the Swap File

sudo chmod 600 /swapfile1

ls -lh /swapfile1

sudo mkswap /swapfile

sudo swapon /swapfile

sudo swapon --show

free -h

Step 5: Make the Swap File Permanent

sudo cp /etc/fstab /etc/fstab.bak

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Step 6: Swap Settings Modification

  • Adjust the Swappiness Property

The swappiness parameter is a tunable kernel parameter. It changes the balance between swapping out runtime memory and accounts for the exchange of data between swaps data out of RAM to the swap space. This parametric value is expressed in percentage, ranging from 0 up to 100.

The vm.swappiness value has a direct influence on the behaviour of system swappiness. The higher the vm.swappiness, the more the system will swap and vice-versa. If the value is close to zero, the kernel will not swap data which may lead to a reduction in performance. Permitting your system not to depend much on the swap is an excellent way to enhance its performance and gain robust responsiveness.

Alternatively, vm.swappiness that is closer towards 100 attempts to put more data in the swap and less on the physical memory. Unlike the low-to-mediocre vm.swappiness value, the high system swapping has very undesirable effects when there are loads of data being swapped actively in-and-out of the RAM.

Check the current swappiness value by typing: cat /proc/sys/vm/swappiness

Set new value for swappiness: sudo sysctl vm.swappiness = 15

or you can add Vm.swappiness = 15 manually into Magic system request Key section to the /etc/sysctl.conf file.       

  • Adjust the Cache Pressure

Next, we are going to modify vfs_cache_pressure that will take care of inode and dentry information over other data. These vfs_cache_pressure settings also control the tendency of the kernel to reclaim the memory used for caching of the directory.

Navigate the proc filesystem again to check the current value:

cat /proc/sys/vm/vfs_cache_pressure

To set new value: sudo sysctl vm.vfs_cache_pressure = 60

or, for the manually add it into /etc/sysctl.conf file. Just below the Vm.swappiness = 15 in Magic system request Key section.

That's it! 

But what about ZFS File System Scenario!!! indecisionangry

Recent Posts

featured_image/2023/01/21/penguin-g2a0df3482_1920.jpg
Gnome Extensions Manual Installation

Gnome Shell extensions can be installed manually. While one can manually put the files in place to install it, the easiest and most fool-proof way is to use the gnome-extensions command line tool.

featured_image/2022/03/24/pexels-david-selbert-7873834.jpg
How to install Windows 11 in KVM? When Ubuntu is your host OS!

Virtualization of new windows OS like 10 and 11 is a challenging task, Only because of now, it's required TPM module for the secure boot process. It's a kind of next-gen security for electronics devic

featured_image/2021/11/25/pexels-david-selbert-7873840.jpg
How to reset GNOME Desktop Settings to Factory Default on Ubuntu 20.04?

There is a simple command that helps you to reset desktop settings. Please read the full post for that command.