Tools like nocache
are actually not the appropriate solution. To cite nocache's source:
What this tool is not good for:
- Controlling how your page cache is used
- Why do you think some random tool you found on GitHub can do better than
the Linux Kernel?
- Defending against cache thrashing
- Use cgroups to bound the amount of memory a process has. See below or
search the internet, this is widely known, works reliably, and does not
introduce performance penalties or potentially dangerous behavior like this
tool does.
So, use cgroups (to be more precise, in 2023 definitely cgroupsv2 whenever possible) to bound the amount of cache your process can use (and thus bound the amount of cache it can evict):
How to run a process and its children in a memory-bounded cgroup
Do this if you e.g. want to run a backup but don’t want your system to
slow down due to page cache thrashing.
If you use systemd
If your distro uses systemd
, this is very easy. Systemd allows to
run a process (and its subprocesses) in a “scope”, which is a cgroup,
and you can specify parameters that get translated to cgroup limits.
When I run my backups, I do:
$ systemd-run --scope --property=MemoryLimit=500M -- backup command
(MemoryMax
for v2)
The effect is that cache space stays bounded by an additional max
500MiB:
Before:
$ free -h
total used free shared buff/cache available
Mem: 7.5G 2.4G 1.3G 1.0G 3.7G 3.7G
Swap: 9.7G 23M 9.7G
During (notice how buff/cache only goes up by ~300MiB):
$ free -h
total used free shared buff/cache available
Mem: 7.5G 2.5G 1.0G 1.1G 4.0G 3.6G
Swap: 9.7G 23M 9.7G
How does this work?
Use systemd-cgls
to list the cgroups systemd creates. On my system,
the above command creates a group called run-u467.scope
in the
system.slice
parent group; you can inspect its memory settings like
this:
$ mount | grep cgroup | grep memory cgroup on
/sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec
latime,memory)
$ cat /sys/fs/cgroup/memory/system.slice/run-u467.scope/memory.limit_in_bytes
524288000
nocache
author, hence the full quotation; I do agree with them, that's what bounding the memory in cgroups does.) Also note that you do not want to completely disable disk caches for things like a backup or an antivirus scan: Readahead caches are very useful in exactly that scenario. – Marcus Müller Jul 06 '23 at 21:50