Recent Linux kernels allow you to measure memory consumption from a QoS point-of-view via pressure stall information: https://www.kernel.org/doc/html/latest/accounting/psi.html
This isn't the same thing as detecting how much memory is available because Linux overcommits by default. PSI will tell you when the kernel is swapping too much (depending on the thresholds you set) and from that you can infer the user is running low on memory.
Android's lowmemkiller had an explicit free memory threshold userspace could set to be notified. Starting with Android 11 however it's now using PSI too IIRC.
IIRC on Linux, it overcommits when allocating, so you can keep mallocing memory beyond capacity and it won't actually fail until you go to use that memory.
Re:Tab Unloading in Firefox 93
by marcosdumay on Tuesday October 05, 2021 @04:21PM
(#28765075)
The GP is describing a signal that your application can react to, not a behavior of allocation failure.
Linux could use some improvement on both. The idea that you always have enough swap and can send everything not used there is wrong. Applications know better how they behave a low-memory than the kernel.
I've longed for such a mechanism in Linux, but I don't think it exists. This should at least be an OS-level signal IMO, though a D-Bus interface could also work. Integrating with one of the recent userspace OOM daemons could be a good idea.