Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evict file from system cache on Linux?

When running performance tests file system cache hit or miss can significantly influence test results. Therefore generally before running such tests used files are evicted from system cache. How to do that on Linux?

Clarification: If possible, the solution should not require root privileges.

like image 809
Paweł Hajdan Avatar asked Oct 02 '08 11:10

Paweł Hajdan


3 Answers

As a superuser you can do the following:

To free pagecache:

  • echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

  • echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

  • echo 3 > /proc/sys/vm/drop_caches

This operation will not "lose" any data (caches are written out to disk before their data is dropped), however, to really make sure all cache is cleaned, you should sync first. E.g. all caches should be cleared if you run

sync; echo 3 > /proc/sys/vm/drop_caches

As I said, only a superuser (root) may do so.

like image 106
Mecki Avatar answered Oct 11 '22 21:10

Mecki


Ha, I have the answer:

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

This is from http://insights.oetiker.ch/linux/fadvise.html

like image 37
Paweł Hajdan Avatar answered Oct 11 '22 22:10

Paweł Hajdan


There is a command line utility by Eric Wong that makes it easy to invoke posix_fadvise:

http://git.bogomips.org/cgit/pcu.git/tree/README

It's then as simple as

$ pcu-fadvise -a dontneed filename-to-evict
like image 43
user143174 Avatar answered Oct 11 '22 22:10

user143174