I wonder if there is a way to flush cpu cache from bash? I noticed there is a solution for this on here, but I don't understand his ruby code and whether this is a correct way of flushing the cache.
-
Why would you want to flush the CPU cache? You might not be able to achieve it completely on any multi-tasking Operating System, as there may be a context switch which will either change which bit of the CPU cache you'll get on return, or invalidate what you'd started to flush. – Rowland Shaw Apr 27 '16 at 11:28
-
@RowlandShaw I am timing a program (trains a model) on a corpus of text. The program starts with lower modelling complexity and increases in complexity gradually. I increase the complexity via setting a parameter which I put in a for loop in a bash script that ranges from low to high complexity. The issue is that the timing reported for each level of complexity must be as-if there is nothing in the cache [to make it a fair comparison across different complexities]. – user3639557 Apr 27 '16 at 11:35
-
you don't need to understand that Ruby code. The code just generates lots of NOPs and useless XOR. You can emulate the same by while looping and doing almost nothing 200000 times. – manzur Apr 27 '16 at 14:52
-
@manzur I got the idea, but why this should work? To be more clear, why this guarantees the entire cache content is filled with new stuff? – user3639557 Apr 27 '16 at 15:00
-
1@user3639557 that code does not guarantee that cache will be flushed. Flushing depends on your CPU and its eviction policy. However, there's `CFLUSH` instruction for x86. Also, this post looks relevant to yours: http://stackoverflow.com/questions/1756825/how-can-i-do-a-cpu-cache-flush-in-x86-windows – manzur Apr 27 '16 at 15:07
-
@manzur, x86's `CLFLUSH` (mind the `L`) will only flush a single cache line. He needs `WBINVD` - http://stackoverflow.com/questions/6745665/wbinvd-instruction-usage – Leeor Apr 28 '16 at 23:53
1 Answers
You can try this command:
sync && echo 1 > /proc/sys/vm/drop_caches
From the documentation on kernel.org:
drop_caches
Writing to this will cause the kernel to drop clean caches, as well as reclaimable slab objects like dentries and inodes. Once dropped, their memory becomes free.
To free pagecache: echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes): echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache: echo 3 > /proc/sys/vm/drop_caches
This is a non-destructive operation and will not free any dirty objects. To increase the number of objects freed by this operation, the user may run `sync' prior to writing to /proc/sys/vm/drop_caches. This will minimize the number of dirty objects on the system and create more candidates to be dropped.
This file is not a means to control the growth of the various kernel caches (inodes, dentries, pagecache, etc...) These objects are automatically reclaimed by the kernel when memory is needed elsewhere on the system.
Use of this file can cause performance problems. Since it discards cached objects, it may cost a significant amount of I/O and CPU to recreate the dropped objects, especially if they were under heavy use. Because of this, use outside of a testing or debugging environment is not recommended.

- 2,400
- 1
- 25
- 39