Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically clear the filesystem memory cache in C++ on a Linux system?

Tags:

c++

c

linux

caching

I'm writing a benchmark tool in C++ where I want to clear the filesystem memory cache between experiments. I'm aware of the following console commands:

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

My question is how can i do this programmatically directly within C++?

Any help is appreciated!

like image 224
Christian Avatar asked Jul 25 '11 15:07

Christian


People also ask

Can we clear cache memory in Linux?

In all the Linux systems we have three options to clear cache without interrupting any services or processes. Now to set run permission, to clear ram cache, you have to call the script whenever required, setting a cron to clear RAM caches every day for 3 hours.


1 Answers

Just write to it :

sync();

std::ofstream ofs("/proc/sys/vm/drop_caches");
ofs << "3" << std::endl;
like image 84
slaphappy Avatar answered Oct 19 '22 22:10

slaphappy