Linux How to reclaim page cache?

What is page cache?
To optimize disk performance Linux caches data of recently read / written files. This part of RAM is called page cache.

Why would you need to reclaim page cache?
By default Linux reclaims page cache using LRU algorithm. While LRU algorithm suffice for general purpose, it may not be suitable for specific requirements of an IO intensive software. In that case the software should implement it’s own logic to reclaim page cache.

How to advice the kernel to reclaim the page cache specific to a file?
posix_fadvise is the system call used to advice kernel about the page cache behavior for the particular file.

If you want to open a file for reading and wish that it should not consume page cache do it following way:

#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[]) {
    int fd;
    int ret;
    fd = open(argv[1], O_RDONLY);
    ret = posix_fadvise(fd, 0,0,POSIX_FADV_DONTNEED);
    return 0;

If you want to reclaim page cache of file being written, it needs to be handled differently. Linux will need the data of file being written in page cache till it does not flush the contents on the disk. Till that point it can not reclaim page cache. So before calling posix_fadvise, you will have to call fdatasync or wait for sufficient amount of time (greater than 30 seconds) before calling posix_fadvise.

Why 30 seconds?
Because max after 30 seconds, Linux treats data in page cache as stale and initiates flushing on disk using pdflush/bdflush thread. The related setting can be found in file:
(default 3000): In hundredths of a second, how long data can be in the page cache before it’s considered expired and must be written at the next opportunity. Note that this default is very long: a full 30 seconds. That means that under normal circumstances, unless you write enough to trigger the other pdflush method, Linux won’t actually commit anything you write until 30 seconds later.

More information can be found at:


How to make sure linux process doesn’t swap out?

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: