Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to know if it's possible to bring pages from a particular process into main memory, or to set parameters for a given process, or class of process so that it will not be swapped out.

share|improve this question

You can unswap a process by reading all of its memory pages.

Normally it would be a bit burdensome (reading /proc/…/maps to learn where are the allocated pages, then reading these pages from /proc/…/mem—browse man proc for more details), but thankfully, there's a program that already does that: gcore. Its goal is to dump core files, and in the process gcore needs to read all that memory. As we're not actually interested in the core file, we can just ask it to store it in /dev/null (core files can be big, so writing them on disk would make it much slower). Example:

gcore 22382 -o /dev/null
share|improve this answer
That sounds sensible. However, gcore (at least on debian) seems to append .[pid] to the file name given by -o, hence the /dev/null trick results in a permission error: Failed to create /dev/null.22382. – mpy Jun 10 '13 at 14:43
@mpy: Interesting. I checked the above command on my Debian Sid system… Maybe it is because the process has multiple threads? – liori Jun 10 '13 at 21:30
I used a simple sleep command to test. But this was on Debian Lenny. Perhaps something has changed, I can recheck on Wheezy later this week. – mpy Jun 10 '13 at 21:42
@mpy: no need to, I can reproduce this now too. Also, I found that gcore is just a shell wrapper over gdb, and that it's the shell wrapper who adds the pid to the file name… so it's a matter of editing that shell script. I'll edit my answer later. – liori Jun 11 '13 at 1:53
Oh yes, you are right (I already upvoted yesterday ;). From the script gdb -> attach PID -> gcore /dev/null -> detach -> quit is the main essence. – mpy Jun 11 '13 at 12:07

If what you're trying to do is improve the performance of a process, I'd recommend nice.

You can also influence the kernel's tendency to swap in general by setting its swappiness.

You might also find this question and its answers helpful.

share|improve this answer
What I'm looking to do is avoid the lag that occurs when a process is accessed for the first time since real memory was full. EG I have a browser tab (in chrome) displaying some static page; I open a gazillion more; main memory fills up and my original tab gets swapped out; I close the gazillion more; I do some stuff; I go to look at the original one again: it has to load from swap. What i'd like to do is just explicitly unswap the pages containing that original tab, ie the process running it (this is in Chrome). – intuited May 22 '10 at 21:15
mlockall() and madvise() seem like they would be applicable if they worked on a per-process basis rather than a per-address basis. I guess I could connect to the process with gdb and run mlockall(). – intuited May 22 '10 at 21:21
It also seems like setting the swappiness doesn't affect how quickly pages are swapped back in? I've changed my swappiness to 0 and still 1/6 of memory contains cache, and about 3% is free. – intuited May 22 '10 at 21:27
@intuited: So you want to look at the original page and it's in swap so you want to bring it out of swap. Well, looking at it will do that. You shouldn't need to do something explicitly that the system is going to do for you (at the same time you'd do it yourself, I might add). Your proposed unswap would have the same kind of lag at the same moment in time that the lag occurs already. Really, Linux does a good job of all this by itself. More RAM is one sure answer. – Dennis Williamson May 22 '10 at 22:38
@Dennis Williamson: The point would be to do it before the system does it. Normally the system will wait until, as I understand it, the pages are needed before bringing them out of swap. I'd like to say "okay, I just closed my huge Audacity project, I have a ton of free memory.. I'm going to bring a bunch of stuff that I'm going to use soon into main memory". While the disk is chugging away I can be working on stuff that's already in memory -- ie stuff that wasn't swapped out while I was working in Audacity. – intuited May 23 '10 at 2:34

You must log in to answer this question.

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