Can I tell Linux not to swap out a particular processes' memory?

Is there a way to tell Linux that it shouldn't swap out a particular processes' memory to disk?

Its a Java app, so ideally I'm hoping for a way to do this from the command line.

I'm aware that you can set the global swappiness to 0, but is this wise?


You can do this via the mlockall(2) system call under Linux; this will work for the whole process, but do read about the argument you need to pass.

Do you really need to pull the whole thing in-core? If it's a java app, you would presumably lock the whole JVM in-core. I don't know of a command-line method for doing this, but you could write a trivial program to call fork, call mlockall, then exec.

You might also look to see if one of the access pattern notifications in madvise(2) meets your needs. Advising the VM subsystem about a better paging strategy might work out better if it's applicable for you.

Note that a long time ago now under SunOS, there was a mechanism similar to madvise called vadvise(2).


If you wish to change the swappiness for a process add it to a cgroup and set the value for that cgroup:

https://unix.stackexchange.com/questions/10214/per-process-swapiness-for-linux#10227


You can do that by the mlock family of syscalls. I'm not sure, however, if you can do it for a different process.


As super user you can 'nice' it to the highest priority level -20 and hope that's enough to keep it from being swapped out. It usually is. Positive numbers lower scheduling priority. Normal users cannot nice upwards (negative nos.)