Software & AppsOperating SystemLinux

How To Configure Swappiness for Optimal Memory Usage on Ubuntu

Ubuntu 17

In this article, we will delve into the topic of configuring swappiness to optimize memory usage on Ubuntu. Swappiness is a Linux kernel property that influences the balance between using RAM for file cache and swapping out pages of memory to disk when the system is under memory pressure.

Quick Answer

To configure swappiness for optimal memory usage on Ubuntu, you can modify the swappiness value in the /etc/sysctl.conf file. This value determines how aggressively the system swaps memory to disk. Lower values prioritize using RAM, while higher values prioritize swapping. Experiment with different values to find the optimal swappiness value for your system’s workload and hardware.

Understanding Swappiness

Swappiness is a value between 0 and 100. A lower value makes the system less inclined to swap and will use RAM as much as possible, while a higher value makes the system more aggressive in swapping out memory to disk. The default value in Ubuntu is 60.

The optimal swappiness value depends on your system’s workload and hardware. For example, systems with plenty of RAM might benefit from a lower swappiness value, while systems with limited RAM might need a higher value to prevent out-of-memory conditions.

Checking the Current Swappiness Value

To check the current swappiness value, you can use one of the following commands:

sysctl vm.swappiness

or

cat /proc/sys/vm/swappiness

These commands will display the current swappiness value. The sysctl command is used to read and write kernel parameters at runtime, while the cat command is used to display the contents of files, in this case, the swappiness value from the /proc/sys/vm/swappiness file.

Changing the Swappiness Value

To change the swappiness value, you will need to modify the /etc/sysctl.conf file. This file is used to configure kernel parameters at boot time.

  1. Open the /etc/sysctl.conf file with a text editor as root. You can use the nano editor for this:
sudo nano /etc/sysctl.conf
  1. Add or modify the line vm.swappiness = X in the file, where X is the desired swappiness value. For example, to set it to 10:
vm.swappiness = 10
  1. Save the changes and exit the text editor. In nano, you can do this by pressing Ctrl+X, then Y to confirm saving the changes, and finally Enter to confirm the file name.

Applying the New Swappiness Value

After changing the swappiness value, you need to apply the changes. You can either reboot your system or reload the sysctl.conf file without rebooting.

To reload the sysctl.conf file and apply the changes immediately, run:

sudo sysctl -p

The -p option makes sysctl read the /etc/sysctl.conf file and apply the changes.

Conclusion

Configuring swappiness can help you optimize memory usage on your Ubuntu system. The optimal swappiness value depends on your system’s hardware and workload, so feel free to experiment with different values to find the one that works best for you.

For more information on swappiness and its effects, you can refer to the Ubuntu SwapFaq or the Linux kernel documentation.

What is the default swappiness value in Ubuntu?

The default swappiness value in Ubuntu is 60.

How can I check the current swappiness value?

You can check the current swappiness value by using the command sysctl vm.swappiness or cat /proc/sys/vm/swappiness.

How do I change the swappiness value?

To change the swappiness value, you need to modify the /etc/sysctl.conf file. Open the file with a text editor as root (sudo nano /etc/sysctl.conf), add or modify the line vm.swappiness = X (where X is the desired swappiness value), save the changes, and exit the text editor.

How do I apply the new swappiness value?

After changing the swappiness value in the /etc/sysctl.conf file, you can either reboot your system or run sudo sysctl -p to reload the sysctl.conf file and apply the changes immediately.

What is the optimal swappiness value for my system?

The optimal swappiness value depends on your system’s workload and hardware. Systems with plenty of RAM might benefit from a lower swappiness value, while systems with limited RAM might need a higher value to prevent out-of-memory conditions. It’s recommended to experiment with different values to find the one that works best for your specific system.

Leave a Comment

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