Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is running sync before drop_caches necessary?

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".

like image 850
Subodh Pachghare Avatar asked Oct 25 '25 19:10

Subodh Pachghare


2 Answers

Writing to drop_caches frees only clean caches.

Executing sync writes back changed data, i.e., converts dirty caches into clean caches.

like image 122
CL. Avatar answered Oct 27 '25 14:10

CL.


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?

like image 35
Subodh Pachghare Avatar answered Oct 27 '25 13:10

Subodh Pachghare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!