How to flush the CPU cache in Linux from a C program?

Rose BEck picture Rose BEck · Jun 30, 2012 · Viewed 36.8k times · Source

I am writing a C program in which I need to flush my memory. I would like know if there is any UNIX system command to flush the CPU cache.

This is a requirement for my project which involves calculating the time taken for my logic.

I have read about the cacheflush(char *s, int a, int b) function but I am not sure as to whether it will be suitable and what to pass in the parameters.

Answer

paulsm4 picture paulsm4 · Jul 1, 2012
  1. I take it you mean "CPU cache", not memory cache

  2. The link above is good: the suggestion "write a lot of data via CPU" is not Windows specific

  3. Here's another variation on the same theme:

  4. Here's an article about Linux and CPU cache:

NOTE:

At this (very, very low) level, "Linux" != "Unix"