title: Clear cache of virtual machine
body: <p>I'm currently doing some database benchmarking on a virtual machine running Ubuntu 12.04. I've noticed that the second time I execute a query it runs significantly faster. This ist most likely due to the OS caching that just keeps all the data in main memory. To keep the cache from screwing up my measurements I thus want to clear it between subsequent runs.</p>
  
  <p>I found the following commands to achieve this on the google: </p>
  
  <pre><code>sync;echo 3 &gt; /proc/sys/vm/drop_caches
  </code></pre>
  
  <p>and</p>
  
  <pre><code>sysctl -w vm.drop_caches=3
  </code></pre>
  
  <p>which all yield in a permission denied error even when I'm logged in as root. It seems like it is just not possible to clear the system's cache from the guest system. I guess this is because it uses the hosts cache. As I don't have access to the host I have to find a workaround. Currently I have two ideas.</p>
  
  <p>First idea is to reboot the machine between runs as this clears the cache. As I want to perform a couple dozen runs I really need to automate this. So I could put a program into autostart let it perform a query and reboot and continue with the next query on the next startup. Feels like writing a virus though.</p>
  
  <p>Second idea is to just flood the memory with other data. As my machine has quite a bit of RAM i would e.g. generate some large file of random data an just read it into /dev/null.</p>
  
  <p>So finally my question is, has anyone a better idea to clear the cache, or maybe avoid the usage of the cache all together? Or has anyone some suggestions on how to implement one of my two ideas easily?</p>
  
  <p>Thank you very much in advance,
  Antigo</p>