Can I write a program, which will launch other small programs that stay in physical RAM and avoid swaps to disk? I'm curious on answers for both Linux and Windows.
-
3Why would you want to do this with today's technology? What's the rationale for the need? – Hovercraft Full Of Eels Feb 22 '13 at 01:31
-
Sure, pin it. Of course to do that you need to do some low-level system calls. – Hot Licks Feb 22 '13 at 01:31
-
For Windows, a pointer and some warnings: http://stackoverflow.com/questions/1039017/can-i-tell-windows-not-to-swap-out-a-particular-processes-memory – Cyrille Ka Feb 22 '13 at 01:32
2 Answers
POSIX systems can lock memory into RAM using mlock
/munlock
. On Windows you can accomplish the same thing with VirtualLock
.
In almost all cases you'll want to let the OS handle that sort of thing, though.
Edit: To elaborate, if your reason for wanting to do this is performance or responsiveness, don't. Pretty much the only reason you would ever want to do this is to stop passwords or encryption keys from accidentally getting written to disk, where an attacker might be able to get at it.

- 25,981
- 9
- 51
- 65
Quoting one of the users:
Yes there is: have the program call the VirtualLock function:
Pages that a process has locked remain in physical memory until the process unlocks them or terminates. These pages are guaranteed not to be written to the pagefile while they are locked.
I believe the SetProcessWorkingSetSize function might also be helpful.
For those people thinking I'm a heretic for even suggesting that this might be useful:
Notice that there are can be valid reasons for locking pages in memory. A valid reason might be that the user is running a CD burner, and the CD burner constantly pages out and hence causes a buffer underrun, creating a coaster. In that situation, It would be valid to lock the memory buffers so that they aren't paged out, even if that means the rest of the computer slows to a crawl, because that's still better than giving the user a coaster. There can be other valid reasons too -- my point is, while it's usually not recommended to lock pages in memory, valid situations still exist, and I don't think killing the idea would help the OP here.
Update: Take a look at Process Hacker's Reduce Working Set menu option.