Subodh Pachghare
Subodh Pachghare

Reputation: 154

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

Upvotes: 4

Views: 5529

Answers (2)

Subodh Pachghare
Subodh Pachghare

Reputation: 154

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?

Upvotes: 3

CL.
CL.

Reputation: 180182

Writing to drop_caches frees only clean caches.

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

Upvotes: 10

Related Questions