23

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
  • 1
    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
  • 1
    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
10

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 is using (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:

#!/bin/sh
renice -n -10 -p `pidof rhythmbox`
ionice -c 1 -n 1 -p `pidof rhythmbox`
| improve this answer | |
5

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.

| improve this answer | |
  • 2
    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
  • 1
    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
  • 6
    "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
  • @bluszcz (well, others reading this given how old that comment is): That might be because it decided that the disk cache for the files served by your httpd is more important than rarely-used parts of your httpd itself - see the other answers mentioning "swappiness". – Jan Schejbal Oct 8 '15 at 17:01
  • 1
    @JanSchejbal thats because linux, at least without someone telling it, doesn't know what processes are important or not. In a server environment, you should be able to tell the system which processes are important and which are not. "I don't care about other things, but these processes are the most important for the operation of this server" – Rahly Jul 29 '16 at 21:08
4

This seems to have already been answered here:

https://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 :/

| improve this answer | |
4

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

From : https://www.kernel.org/doc/Documentation/sysctl/vm.txt

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: https://unix.stackexchange.com/questions/10214/per-process-swapiness-for-linux#10227

| improve this answer | |
  • 1
    this affects swappiness globally, not process-specific. – Lorenzo Von Matterhorn Apr 14 '13 at 22:57
  • 1
    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
  • Then that's no help, is it? – Ken Sharp Dec 18 '16 at 1:31
  • Ken Sharp: This answer is the only one linking directly to what seems (at least at first glance) like the best solution to the problem. So... pretty much the opposite of what you said. – phils Mar 22 '17 at 22:19
  • @phils You don't get how this works. – Ken Sharp Mar 31 '17 at 4:47
2

You can use mlockall() syscall. mlockall() force memory process to be resident (= no swap, no overcommit, etc...). AFAIK, there is no shell command to do it, but it easy to create one. It would look like:

if (mlockall(MCL_FUTURE | MCL_CURRENT))
     perror("mlockall");
execvp(argv[0], argv);

Note, you need to be root to call mlockall().

However, as said in other answer, I don't think it is really what you want.

| improve this answer | |
  • 1
    The code will not work at all as execvp is a frontend to execve which undos mlockall. See Notes section on mlock man-page. – Johannes Matokic Mar 27 '19 at 17:19
  • Upvoting this because anyone making an audio streaming library must do mlockall to ensure the problem I had doesn't happen. – Alexei Averchenko Feb 21 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.