# How to prevent a specific program from swapping?

Is it possible to prevent a specific program (i.e. rhythmbox and its dependencies) from ever swapping to disk?

I'm asking because I have a problem when a music player hiccups whenever Chromium hogs too much memory. Is there a way to work around this? This isn't a problem in Windows anymore so presumably there is a way.

-
Tomboy has the same problem. It's one of the reasons some people have switched to GNote. –  Cristian Ciupitu Feb 9 '11 at 3:28
I think that the hiccup problem is not necessarily caused by swapping. If the program is playing something, Linux should notice this and not swap it. Programs that don't do much are the first ones that get swapped. Have you run ps or top to see if rhytmbox is really swapped by checking the RSS/RES field? I think that your problem is mostly caused by improper scheduling. You should try to renice the rhytmbox process or change some of its settings, e.g. the size of the audio buffer. –  Cristian Ciupitu Feb 9 '11 at 3:37
Thanks! Is there a way to set up the initial nice value of a program? /etc/nicetab or something? :) –  Alexei Averchenko Feb 9 '11 at 4:07

I think that the hiccup problem is not necessarily caused by swapping. If a program is playing something, Linux should notice this and not swap it. Programs that don't do much are the first ones that get swapped. You can check if the program is really getting swapped by looking at the RSS/RES field from ps or top. RSS is the resident set size, the non-swapped physical memory that a task has used (in kiloBytes)).

I think that your problem is most probably caused by improper CPU and I/O scheduling and a bit of inefficiency of Rhythmbox which makes it sensitive to high system loads. The CPU priority can be changed with the commands nice and renice. The I/O priority can be changed with the ionice command. Only the super user can use high priorities. You should also know that Linux kernel guys are trying to improve the responsiveness of desktop systems with various low-latency patches, so you might consider using them. One of them is a ~200 lines patch written by Mike Galbraith which has impressed even Linus. The alternative to this patch is Lennart Poettering's cgroups trick which I think will be the default in Fedora 15.

Anyway, without those patches there are two options: start the program with a high priority or change it afterwards. For the first option you could use a wrapper script around Rhythmbox:

#!/bin/sh
# Run Rhytmbox with high CPU and I/O priorities
nice -n -10 ionice -c 1 -n 1 su -l -c rhythmbox alexei


You will need to run it as root. If you don't want to login as root just to start this, you can use either su or sudo.

As for changing the priority afterwards, if you're too lazy to login as root to change it, you could try using a cron job that runs every 5 minutes and sets the priority of the rhythmbox process, but I wouldn't recommend doing this.

-

Short answer: You can't, and shouldn't.

A 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.

If you really don't want to swap, get more RAM, or just # swapoff -a (not recomended, can turn your system unusable if you already have problems).

Shouldn't goes when you are developing some app and don't want it to swap at all. Take a look at this post on stackoverflow.

-
I don't think that reserving around 100 MiB will kill my system. Maybe there is another way to solve my particular problem (see edited question)? –  Alexei Averchenko Feb 9 '11 at 2:51
If you intend to keep a process on a high level of responsiveness over other processes, the answer is nice (you must be root to do this). Messing around with memory paging is not easy if you don't want to get your hands dirty (touch the code, recompile, or even LD_PRELOADing a custom library to fool the process and tweak the function used to allocate memory - again, no recommended). Advice ? renice the process, get more RAM, or stop opening tabs ;) –  Torian Feb 9 '11 at 3:28
"If the kernel decides it has to swap, it sure has a valid reason" that's not true. Today I had 1,3G free memory. Linux kernel put my httpd processes into the swapping (370M). –  bluszcz Aug 6 '12 at 15:28

http://stackoverflow.com/questions/578137/can-i-tell-linux-not-to-swap-out-a-particular-processes-memory

Though the consensus is that you probably shouldn't :/

-

There are several ways to do that. You can attempt to try is "says" to Linux work less with swap (generally):

echo 10 > /proc/sys/vm/swappiness


swappiness

This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase agressiveness, lower values decrease the amount of swap.

The default value is 60.

Other option is use the cgroups kernel manager , this is per-process specific but you will have some "work" to do: Answered here: http://unix.stackexchange.com/questions/10214/per-process-swapiness-for-linux#10227

-
this affects swappiness globally, not process-specific. –  Lorenzo Von Matterhorn Apr 14 '13 at 22:57
yes , you can have notice that on "(generally)" I wrote. The option to be per-process is the link to other answer, using cgroups. –  ceinmart Apr 15 '13 at 2:44