When a Linux operating system and user processes in the operating system run, the page cache (also known as file cache) is utilized. The page cache usage increases over the runtime, which leads to high system resource consumption. This may cause performance jitter for performance-sensitive business or even cause out-of-memory (OOM) errors to occur in specific tasks. In business scenarios in which you must release a large amount of memory, you can call the /proc/sys/vm/drop_caches interface to release the page cache.
Procedure
Memory reclamation by calling the drop_caches interface deletes page cache and the required dentries and inodes from the system, which may cause performance degradation during disk I/O operations. The kernel must re-read this data from disk on the next access, which can cause significant I/O and CPU overhead, especially for data under heavy use. Exercise caution when you reclaim memory based on your business requirements.
The following operations release only clean (already written to disk) cached data. Dirty pages in memory are not released until they are written to disk. To release dirty pages from memory, run the sync command to write the dirty pages from memory to disk and then call the drop_caches interface to clear the page cache. This way, you can release more memory.
Cache regrows automatically after clearing. The kernel immediately begins caching data again as applications access files. Each drop_caches operation also produces an informational message in the kernel log (for example, sh (1234): drop_caches: 3). These messages are normal and do not indicate a problem.
| Value | Releases | Use case |
|---|---|---|
1 | Page cache | Free file data cache (most common) |
2 | dentries and inodes | Free filesystem metadata cache |
3 | Page cache, dentries, and inodes | Free all reclaimable cache |
Release the page cache.
Call the
drop_cachesinterface to release the page cache.sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'Run the
sysctlcommand to release the page cache.sudo sysctl -w vm.drop_caches=1
Release
dentriesandinodes.Call the
drop_cachesinterface to releasedentriesandinodes.sudo sh -c 'echo 2 > /proc/sys/vm/drop_caches'Run the
sysctlcommand to releasedentriesandinodes.sudo sysctl -w vm.drop_caches=2
Release the page cache,
dentries, andinodes.Call the
drop_cachesinterface to release the page cache,dentries, andinodes.sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'Run the
sysctlcommand to release the page cache,dentries, andinodes.sudo sysctl -w vm.drop_caches=3