I understand that when RAM fills up and the kernel starts moving pages back and forth from disk to RAM, programs will become less responsive. In my case however the whole system freezes (or lags horribly, with the mouse moving every 10 seconds or so) every time it goes into swap. I assume this is because the OS is either too busy moving stuff or it's moving its own parts to swap.

Why can't we protect the main components of the OS including the graphical interface (X) from swapping (by keeping them fixed in RAM) so that we have at least a responsive OS while we suffer lags in the rest of our applications? (Or if the issue is the kernel being too busy, setting the swapping priority lower?)

2 Answers 2


First of all, you can change "swappiness" - how eager is your OS to use swap - using this one-liner:

sudo bash -c "echo 'vm.swappiness = 15' >> /etc/sysctl.conf"

As for why is OS not locking X server, the answer is simple: it's only your use case. See, if I am computing something with my script, I want it to be as fast as possible(and end as quickly as possible) - preserving unneccessary programs in the memory would mean that it's my program that has to use swap, making it many orders of magnitude slower.

Many users who run such memory-heavy scripts either don't use X at all or don't care about them, because they leave their PCs alone.

tl;dr: it's a design decision that's complex and the current solution is probably the best.

  • The issue though is that I don't think it's possible to lock X in RAM even if I wanted to at least according to @ashish-k who says that the +t is ignored. I understand that some users might want to prioritize x over y but I believe that the more common user case would prefer to have a responsive OS. So it would be at least nice to have an option. Thanks for the insight.
    – Stefan
    Mar 28, 2017 at 14:53
  • The best option is to not use swap tbh. I prefer my script to use RAM than swap, since swap is SOOO slow... I hope the option exists, but I do not know it. Good luck :) Mar 28, 2017 at 15:18

If you talking about protecting the main components of the OS but disabling the swap on them, I would prefer you shoudn't (anyway you can't as long time ago executable files honored the sticky bit +t which would tell the kernel not to swap, but today it is ignored). If the kernel decides it has to swap, it sure has a valid reason. Linux is very aggressive on memory usage, because RAM that is idle, is a wasted resource.

Now, if at you decide to turn off the swap space as a whole, you can do that by -

To disable a swap area : swapoff [-v] specialfile...

To disable all swap area : swapoff -a

Also look at this answer for more information.

  • Interesting. Thanks for you answer! My issue is a) I'm not super-user b) I don't think a python session loading gigabytes of data in memory to be a valid reason to block the whole OS. Disabling swap is also not really a solution since often I go barely over the RAM and after a few minutes it gets unstuck and I have my results. I would prefer having swap but at lower priority than the OS key components as I'm sure most Linux users would.
    – Stefan
    Mar 28, 2017 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .