{"id":3749,"date":"2017-04-03T18:08:53","date_gmt":"2017-04-03T12:38:53","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3749"},"modified":"2017-04-03T18:08:53","modified_gmt":"2017-04-03T12:38:53","slug":"clean-cache-linux-programmatically","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/clean-cache-linux-programmatically\/","title":{"rendered":"[ Solved -3 Answers] How to clean cache in Linux programmatically"},"content":{"rendered":"<p><label class=\"label label-warning\">PROBLEM:<\/label><\/p>\n<p>We write a benchmark tool in C++ where we want to clear the filesystem memory cache between experiments.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">sync<br\/>echo 3 &gt; \/proc\/sys\/vm\/drop_caches<\/code><\/pre> <\/div>\n<p>My question is how can we do this programmatically directly within C++?<br \/>\n<label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<p><b>Try this :<\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">sync();<br\/><br\/>std::ofstream ofs(&quot;\/proc\/sys\/vm\/drop_caches&quot;);<br\/>ofs &lt;&lt; &quot;3&quot; &lt;&lt; std::endl;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<p><b>Try this sample code:<\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">int fd;<br\/>char* data = &quot;3&quot;;<br\/><br\/>sync();<br\/>fd = open(&quot;\/proc\/sys\/vm\/drop_caches&quot;, O_WRONLY);<br\/>write(fd, data, sizeof(char));<br\/>close(fd);<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<h5 id=\"clean-page-cache-in-linux-programmatically\"><span style=\"color: #ff6600;\"><b>Clean page cache in <\/b><b>linux<\/b><b> programmatically<\/b><\/span><\/h5>\n<ul>\n<li>You are probably stuck with opening the file \/proc\/sys\/vm\/drop_caches, writing 1 to it and close it again. There is no dedicated syscall for that operation.<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">sync();<br\/>int fd = open(&quot;\/proc\/sys\/vm\/drop_caches&quot;, O_WRONLY);<br\/>write(fd, &quot;1&quot;, 1);<br\/>close(fd);<\/code><\/pre> <\/div>\n<p>Depending on what you try to achieve, the (optional) preceding sync() can help free some more memory.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>How to clean cache in Linux programmatically- write a benchmark tool in C++ clear the file system memory cache between<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1699],"tags":[7398,7406,7414,7400,7415,7402,7405,7423,7404,7408,7413,7412,7397,7424,7417,7401,7403,7394,7392,7407,7399,7409,7410,7422,7418,7411,7420,7416,7419,7421,7395,7393,7396],"class_list":["post-3749","post","type-post","status-publish","format-standard","hentry","category-linux","tag-c-flush-cache","tag-cache-server-linux","tag-cached-memory-linux","tag-cacheflush-example","tag-clean-linux","tag-clear-cache-chrome","tag-clear-cache-in-linux","tag-clear-dns-cache-linux","tag-clear-linux","tag-clear-server-cache-linux","tag-dns-cache-linux","tag-dns-linux","tag-flush-cpu-cache-linux","tag-flush-dns-cache-linux","tag-flush-dns-linux","tag-flush_cache_all","tag-how-to-clear-cache-in-linux","tag-how-to-flush-cache","tag-linux-flush-processes-consume-too-much-of-cpu","tag-linux-cache-server","tag-linux-cacheflush","tag-linux-cleaner","tag-linux-clear","tag-linux-clear-cache","tag-linux-clear-dns-cache","tag-linux-dns","tag-linux-dns-cache","tag-linux-flush-dns","tag-linux-flush-dns-cache","tag-linux-free-memory","tag-networking-how-do-i-clear-the-dns-cache","tag-the-linux-cache-flush-architecture","tag-using-the-application-cache"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3749"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3749\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}