27

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.

4
  • Tomboy has the same problem. It's one of the reasons some people have switched to GNote. Feb 9, 2011 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. Feb 9, 2011 at 3:37
  • 1
    Thanks! Is there a way to set up the initial nice value of a program? /etc/nicetab or something? :) Feb 9, 2011 at 4:07

5 Answers 5

12

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`
5

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

6
  • 3
    this affects swappiness globally, not process-specific. Apr 14, 2013 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, 2013 at 2:44
  • Then that's no help, is it?
    – Ken Sharp
    Dec 18, 2016 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, 2017 at 22:19
  • @phils You don't get how this works.
    – Ken Sharp
    Mar 31, 2017 at 4:47
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 :/

3

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.

7
  • 3
    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)? Feb 9, 2011 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, 2011 at 3:28
  • 8
    "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, 2012 at 15:28
  • 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, 2016 at 21:08
  • 1
    "If the kernel decides it has to swap, it sure has a valid reason." Well, the kernel does not know everything, thus sometimes makes bad choices. I have a daemon that needs to be very responsive when it gets a connection. While it doesn't take much memory (10 MB RSS), it occasionally gets swapped out (probably because it is waiting most of the time), so that it is too slow to react. Note: because RAM costs money, swap is useful as the end user may sometimes need more memory than usual for a non-critical usage. This should not affect the system.
    – vinc17
    May 2, 2020 at 10:12
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.

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

You must log in to answer this question.

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