Q. On Linux I’m working on a program, which uses lot of RAM (~8GB) for database. That program also generates huge file I/O activity, leading to use of great amount of page cache (~25GB). The program used to run for days, and I observed that db was not performing as expected. I realized this was because RAM pages of the process was getting swapped out. On Linux how you can prevent swapping out of RAM pages of a process?
Swappiness is a Linux kernel parameter that controls the relative weight given to swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive. A low value causes the kernel to avoid swapping, a higher value causes the kernel to try to use swap space. The default value is 60
You can check your swappiness value by:
user@machine:$ cat /proc/sys/vm/swappiness
You can change swappiness value by:
# Set the swappiness value as root
echo 10 > /proc/sys/vm/swappiness
One way to make sure that RAM pages assigned to running process should not swap out, is to set the sappiness value low, less than or equal to 10.
This will result in to global effect and will protect all the running processes on the system from getting swapped out. This may not be of your interest, if you wish to protect only a particular process. This can be solved by second approach.
mlockall pins all the RAM pages of a running process. For a particular process you can prevent swapping out of existing and future RAM pages by calling:
mlockall(MCL_CURRENT | MCL_FUTURE);