title: How do I invalidate all (data) caches from the command line?
body: <p>For some precise measurements, I would like to invalidate/flush all caches up to RAM (main memory), from the command line (so that the main program running time evaluation is not affected by this process). I have found the following (The first and last from <a href="https://stackoverflow.com/q/3446138/50305">here</a>):</p>
  
  <pre><code>1. echo 3 &gt; /proc/sys/vm/drop_caches
  </code></pre>
  
  <p>and I could build a (pre-executed) program with the following</p>
  
  <pre><code>2. #include &lt;asm/cachectl.h&gt;
  int cacheflush(char *addr, int nbytes, int cache);
  </code></pre>
  
  <p>or I could finally do a </p>
  
  <pre><code>3. int main() {
       const int size = 20*1024*1024; // Allocate 20M. Set much larger then L2
       char *c = (char *)malloc(size);
       for (int i = 0; i &lt; 0xffff; i++)
         for (int j = 0; j &lt; size; j++)
           c[j] = i*j;
   }
  </code></pre>
  
  <p>My question is: for what I need to do, which version is best, and if it is #2, what is the address I should be giving it as a start address? My <code>uname -a</code> is Linux 3.2.0-33-generic #52-Ubuntu SMP Thu Oct 18 16:19:45 UTC 2012 i686</p>