It's levels of abstraction. The kernel does not know that swapping out X11/window-manager/gnome-terminal/whatever will make your system unusable.
There is mlockall() but it's a loaded footgun by default, and is rlimit-ed by default because it's not something you want users on a multiuser system to be able to do willy-nilly.