tags: - ubuntu categories: - informational comments: true

date: 2022-04-09 00:00:00

DESCRIPTION

In case of lack of system memory, add a file system based swap file. Ensure there is atleast 1GB disk space to create a swap file.

ERRORS

VERIFICATION

swapon --show

COMMANDS

Create

fallocate -l 1G /swapfile
dd if=/dev/zero of=/swapfile bs=1024 count=1048576
chmod 600 /swapfile
mkswap /swapfile

Enable

swapon /swapfile

Mount

vi /etc/fstab /swapfile swap swap defaults 0 0

Tuning swap

Direct quote from Ubuntu web page

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk.

Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

swappiness can have a value of between 0 and 100

swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible

swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache

The default setting in Ubuntu is swappiness=60.

Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation.

A value of swappiness=10 is recommended, but feel free to experiment.

Note: Ubuntu server installations have different performance requirements to desktop systems, and the default value of 60 is likely more suitable.

To check the swappiness value

cat /proc/sys/vm/swappiness

To change the swappiness value A temporary change (lost on reboot) with a swappiness value of 10 can be made with

sudo sysctl vm.swappiness=10

To make a change permanent, edit the configuration file with your favorite editor:

cat /etc/sysctl.d/100-swap-manager.conf 
vm.swappiness=60
vm.vfs_cache_pressure=120

sysctl -p

or reboot

What is vm.vfs_cache_pressure ?

Quote

At the default value of vfs_cache_pressure=100 the kernel will attempt to reclaimd entries and inodes at a “fair” rate with respect to pagecache and swapcache reclaim.

Decreasing vfs_cache_pressure causes the kernel to prefer to retain dentry and inode caches.

When vfs_cache_pressure=0, the kernel will never reclaim dentries and inodes due to memory pressure and this can easily lead to out-of-memory conditions.

Increasing vfs_cache_pressure beyond 100 causes the kernel to prefer to reclaim dentries and inodes.

References

https://help.ubuntu.com/community/SwapFaq https://docs.gluster.org/en/main/Administrator-Guide/Linux-Kernel-Tuning/