I have read a lot of docs saying, it is good idea to do a "sync" before doing echo [1,2,3] > /proc/sys/vm/drop_caches. I am not able to understand why it is needed, drop_cache is a non-destructive operation, dirty data is not going to be deleted by drop_cache. I have also seen a behavior where echo 1 > /proc/sys/vm/drop_caches first commit dirty data back to disk and then frees the cache. This is seen through /proc/meminfo "Dirty" & "Writeback".
Writing to drop_caches frees only clean caches.
Executing sync writes back changed data, i.e., converts dirty caches into clean caches.
I corroborated this by doing a small experiment.
"sync" only makes dirty cache to clean cache. cache is still preserved. drop_caches doesn't touch dirty caches and only drops clean caches. So to make all memory free, it is necessary to do sync first before drop_caches in case flushing daemons hasn't written the changes to disk.
My blog about this little experiment -
What are exactly O_DIRECT, O_SYNC Flags, Buffers & Cached in Linux-Storage I/O?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With