OSX handles this with a kqueue event that can notify userland when the system moves between various memory pressure states; this is hooked into by libdispatch and other userland libraries which will discard caches and so on.
I don't see why Linux couldn't do the same; open /sys/kernel/something and epoll on it.