I came across this . And then remembered seeing this something back. Could anybody elaborate is there any need for a user to ever flush the caches or it's only need is when you want to benchmark systems or are there any other usage scenarios for flushing caches ?
3 Answers
There is a need to invoke sync
when you need to ensure that any dirty sectors are flushed to the media.
One of the common cases is when you dd
an fs
image to the usb
flash stick and do not provide conv=fdatasync
option (that is missing in most of recipes published here and there): the flash device write speed is very low, so it could take a few minutes to get everything written especially if the stick had many sectors written before.
The sync
program ensures that everything is flushed upon its termination.

- 8,541
Other than benchmarking, I know of no scenario where you would need to flush the caches. Linux caches are cleverly managed, and the memory they use are always available on demand. So you probably won't achieve anything by flushing them other than slowing your system.
For a good reading on the matter, see this webpage.

- 17,793
- 2
- 53
- 69
Serge and xhienne have already said important things about the cache management in Linux, in particular that it is hard to gain anything by interfering.
However, there are special cases apart from benchmarking where you can benefit from dropping caches:
For example, I have a notebook with a large RAM (32 GB). Of course, Linux takes advantage of it, which is fine. But when it comes to hibernating the system, all valid memory contents has to be written to a persistent storage—swap in my case. Therefore, freeing up some memory in advance can accelerate the hibernate process tremendously.
That's why I have
echo 3 > /proc/sys/vm/drop_caches
in one of my hibernate hooks, closely after a sync
. The gain in speed is up to half an order of magnitude in my case.
The same holds for resuming from hibernate, of course.
Caveat: After resuming from hibernate, the system reacts notably slower until the caches are filled up again.

- 702
sync
flushes write buffers and does not affect caches. But the two are so often confused that it's a good idea to mention this here. – Gilles 'SO- stop being evil' Jan 11 '17 at 23:23