Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

swap

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?

like image 919
sanity Avatar asked Feb 23 '09 15:02

sanity


People also ask

How do I make sure that the swap memory used by my application is not flushed away by any other process?

To clear the swap memory on your system, you simply need to cycle off the swap. This moves all data from swap memory back into RAM. It also means that you need to be sure you have the RAM to support this operation. An easy way to do this is to run 'free -m' to see what is being used in swap and in RAM.


4 Answers

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

like image 126
Thomas Kammeyer Avatar answered Nov 12 '22 16:11

Thomas Kammeyer


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

like image 21
Matthew Buckett Avatar answered Nov 12 '22 15:11

Matthew Buckett


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

like image 43
jpalecek Avatar answered Nov 12 '22 16:11

jpalecek


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

like image 29
SumoRunner Avatar answered Nov 12 '22 16:11

SumoRunner