On a box that walks huge folder trees, like a media library or a backup target, every directory lookup needs the disk again once the kernel has dropped that cache. One sysctl tells it how hard to throw that cache away.
sysctl vm.vfs_cache_pressure
The default is 100 and the output looks like this:
vm.vfs_cache_pressure = 100
The kernel docs describe it as the tendency to reclaim the memory used for caching directory and inode objects. At 100 the kernel reclaims dentries and inodes at a fair rate compared with page cache and swap cache. A lower value makes it keep them longer. A value above 100 makes it prefer reclaiming them.
sudo sysctl -w vm.vfs_cache_pressure=50
That applies at once and is gone after a reboot. To keep it, put the line in a file under /etc/sysctl.d/ that ends in .conf, for example /etc/sysctl.d/99-cache.conf:
vm.vfs_cache_pressure = 50
Then load it with sudo sysctl --system.
You can see how much of this memory the kernel considers reclaimable in /proc/meminfo:
grep -E 'Slab|SReclaimable' /proc/meminfo
SReclaimable is the part of the slab memory that might be reclaimed, such as caches.
The caveats are written into the docs. At 0 the kernel never reclaims dentries and inodes under memory pressure, and that can easily lead to out-of-memory conditions. Very high values can hurt performance, because the reclaim code has to take locks to find objects to free. Newer kernels also have vm.vfs_cache_pressure_denom, which defaults to 100, and the setting is relative to it. I’d leave the default alone until there is a reason, and then move in small steps.
Further reading: Kernel documentation: sysctl vm, proc_meminfo(5) man page