Is there any way a running program can detect in a standardised way that there is "memory pressure", by which I mean, a shortage of memory in the machine, so that it can return memory (by reducing the size of a cache, for example) to the operating system and thus alleviate the pressure?
Protocols for this purpose existing on certain platforms — for example, on macOS right now you can see
NSPurgeableData, and I believe similar concepts existed all the way back to the earliest days of the Macintosh operating system (where memory pressure was an even more severe concern), and I'd guess that Windows and Android have similar things — but I haven't heard of any cross-platform approaches to this.
you like GitHub - koute/bytehound: A memory profiler for Linux. I guess
MADV_FREE which can be used to implement a similar thing to Apple's
NSDiscardableContent, but different.
However, using either of these safely (in the sense of memory safe) is challenging, since you're effectively giving the OS the right to delete the memory content "behind your back" but only if it feels like it, leading to interesting interactions with
Drop if you're not careful.
Current Linux also has pressure stall information via a file descriptor you can
poll() to know about memory pressure; systemd has pressure stall handling to point you at the right file to open and poll (since you might be in a restricted cgroup), but I'm not aware of any equivalent on other OSes.
Looks like it's CreateMemoryResourceNotification on Windows.