Wednesday, January 21, 2009

Locking applications in memory.

Have you ever been using your computer, and all of a sudden, everything locks up? How about everything just gets really slow? Anyone remember the warning that the early versions of Windows would give you about being "out of virtual memory"? Well I still see this problem. For me, Firefox periodically tries to take >2gb of ram[1]. Since my machine only has 2gb of ram, when that happens, my computer slows to a crawl or even locks up until it is out of swap and the kernel kills firefox.

Usually, the only thing I want to do when my computer starts slowing down is find out the culprit and kill it. All I really need for that is my terminal[2]. So I thought "I wonder if it is possible to prevent my terminal from being swapped out?" With some investigation, I discovered mlock(2) which allows you to do just that! So I just modified my terminal a bit so that it mlocks itself when it starts.

Now my terminal is a little special. I use rxvt-unicode which includes a special two part option: a daemon, and a client. The daemon sits in memory with a full working copy of the terminal, and the client just spawns new instances of that background terminal. The benefit of this is twofold: one, faster bootup... when you start a new client, it only has to read ram to start, instead of having to read from the disk, and two, the read-only parts of the terminal can be (and are) shared among all the clients.

So what i'm saying is that I didn't actually modify the terminal, I modified the daemon. So the patch here modifies the daemon to add the -l or --lock option which calls mlockall to lock itself in memory.


[1] Firefox may not be the real culprit... it may be a plugin (flash).
[2] That's not actually true. I also need things like bash, top, kill, and whatever is interpreting my keystrokes.

No comments: