Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to purge disk I/O caches on Linux?

I need to do it for more predictable benchmarking.

like image 971
taw Avatar asked Mar 04 '12 02:03

taw


People also ask

Should I clear cache on Linux?

It is generally safe to delete it. You might want to close all graphical applications (e.g. banshee, rhythmbox, vlc, software-center, ..) to prevent any confusion of the programs accessing the cache (where did my file go all of a sudden!?).

How do I free up memory on Linux?

The most common way you'll see on the web to check for free memory in Linux is by using the free command. Using the free -m command to check your Linux memory usage, displays the values as MB instead of KB. The free column beside -/+ buffers/cache with 823 MB is the actual free memory available to Linux.

How do I clear cache on Linux app?

Alternatively, right-click on the . cache folder itself with the mouse and select “Move to trash” to completely delete all cache files on your Linux PC.


2 Answers

Sounds like you want the sync command, or the sync() function.

If you want disk cache flushing: echo 3 | sudo tee /proc/sys/vm/drop_caches

like image 86
Chris Dennett Avatar answered Oct 16 '22 22:10

Chris Dennett


You can do it like this:

# sync # (move data, modified through FS -> HDD cache) + flush HDD cache # echo 3 > /proc/sys/vm/drop_caches # (slab + pagecache) -> HDD (https://www.kernel.org/doc/Documentation/sysctl/vm.txt) # blockdev --flushbufs /dev/sda # hdparm -F /dev/sda  # NEXT COMMAND IS NOT FOR BENCHMARKING: # should be run before unplug, flushes everything possible guaranteed. # echo 1 > /sys/block/sdX/device/delete 

You may use strace to see that these are three different syscalls

Also, it may be desirable to turn off HDD cache using hdparm, not sure what thing you benchmarking.

In any way, you cannot prevent HDD to cache last 64/32/16 MB of recently used data. In order to kill that cache, just write some amount of zeroes (and flush) + read some unrelated place from HDD. This is required since cache may be divided to read-part and write-part. After that you can benchmark HDD.

like image 41
socketpair Avatar answered Oct 17 '22 00:10

socketpair