What is the purpose of /proc/sys/vm/drop_caches in Red Hat Enterprise Linux?
Environment
- Red Hat Enterprise Linux
Issue
- What's the purpose of
/proc/sys/vm/drop_cachesin Red Hat Enterprise Linux? - How to release cache memory using /proc/sys/vm/drop_caches?
- New process cannot be started due to insufficient free memory, how to free additional memory?
- Inactive memory not being released, how to free memory?
Resolution
Background
-
In order to speed up operations and reduce disk I/O, the kernel usually does as much caching as it has memory. By design, pages containing cached data can be repurposed on-demand for other uses (e.g. apps). Repurposing memory for use in this way is no slower than claiming pristine untouched pages.
-
Writing to
/proc/sys/vm/drop_cachesallows one to request the kernel immediately drop as much clean cached data as possible. "Clean" cached data is eligible for dropping. "Dirty" cached data needs to be written somewhere. Usingvm.drop_cacheswill never trigger the kernel to drop dirty cache. -
This will usually result in some memory becoming more obviously available; however, under normal circumstances, this should not be necessary. For an exception to this, see solution The dentry_cache size continually grows on Red Hat Enterprise Linux.
-
Also, note that in RHEL5.2 and earlier, using
vm.drop_cachescan cause a deadlock if the system is under heavy memory and IO load.- Red Hat Enterprise Linux 4.6 and 5.0 introduced /proc/sys/vm/drop_caches.
- There were issues in releases 4.6 through 5.2 that could cause deadlocks between
kjournaldanddrop_pagecacheif the system was under heavy io load. - This issue persists within RHEL 4 releases but was fixed in RHEL 5.3 and later. Reference This content is not included.Bug 444961 - softlockup when repeatedly dropping caches and This content is not included.Bug 495861 - A deadlock can occur between kjournald and drop_pagecache.
-
If a GFS2 filesystem is mounted on the host, review:
Should the cache be dropped on a GFS2 file-system when a performance issue is occurring?
Commands
-
Optional : Start with the
synccommand to have the kernel write as many dirty cache pages to disk as it can (to maximize the number of data cache pages that can be dropped). -
Trigger cache-dropping by using
sysctl -w vm.drop_caches=<number>or by writing directly to/proc/sys/vm/drop_cachesas follows:-
Free pagecache, dentries and inodes
echo 3 >/proc/sys/vm/drop_caches -
Free dentries and inodes only
echo 2 >/proc/sys/vm/drop_caches -
Free the pagecache only
echo 1 >/proc/sys/vm/drop_caches
-
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.