Linux uses spare memory to cache recently accessed files and filesystem information. An administrator may clear these caches for a controlled test, but Linux normally reclaims them automatically when applications need memory.
When cache clearing is useful
Use cache clearing only when a specific test needs file data to be read again from storage. A low free figure alone is not a reason to clear it; look at available memory and the application’s behaviour.
How Linux caching works
The page cache holds file contents, while dentries and inodes cache filesystem metadata. Dropping them does not free memory still used by applications, fix a memory leak or clear every application cache.
Check current memory usage
Record the current figures:
free -hClear caches for a controlled test
You need root or sudo access. Plan the test during a suitable maintenance period.
Dropping caches can increase disk activity and slow applications while data is loaded again. Avoid doing this routinely on a production server.
-
Write pending filesystem data before the test:
sudo sync -
Choose one value for the cache you need to drop:
Value Cache affected 1Clean file page cache 2Reclaimable dentries and inodes 3Both of the above -
For example, to drop both types of clean, reclaimable cache:
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
The privileged shell is needed for the redirection. sudo echo 3 > ... does not make your current shell’s write privileged. Dirty or in-use objects are not simply discarded by this operation.
Avoid routine scheduled clearing
Do not add a nightly cache-clearing job as a general memory fix. Repeatedly discarding useful cache can reduce performance and hide the problem you need to diagnose. Investigate application memory growth or sustained low available memory instead.
Verify memory after clearing cache
Run free -h again and compare the result. Cache figures may change quickly as applications continue to work. A smaller cache is not evidence that the underlying performance problem has been resolved.
Best practices
Record why the test was required, monitor its effect and allow normal caching to resume. If the server is under memory pressure, investigate the processes and workload rather than repeatedly dropping caches.