2010-01-19

Linux swap: an advice

In the olden days, the Linux enthusiasts would recommend that you should create a SWAP partition that has twice the size of your RAM.  Nobody knew why, but it just a rule we all followed.  In those days, computers had something like 32 MB of RAM.  Using 64 MB of disk space as SWAP seemed OK.

Today I opened a complex diagram in PDF format, generated by a computer program, with the Ubuntu PDF reader, Evince.  The last setting Evince used for that file was with a very high zoom level, and so Evince tried to render the PDF page with that zoom level.  After a few seconds I lost control of the computer due to thrasing.  How can this be?  The software security guys keep a constant watch on program flaws that make DoS attacks possible when opening a malicious file.  I have generated a DoS attack to myself?!

When I regained control of the computer I discovered I had 3 GB of swap space installed.  When a program is buggy and tries to allocate huge chunks of memory, of course it will bring the system to its knees.

My solution: reduce the swap space to 512 MB.  Now Evince just fails to allocate memory and does not render the page.  But I regain control and can switch to a different zoom level and finally render my page.  A lesson learnt.

4 comments:

Tiago Cogumbreiro said...

You do not use hibernate, right?

Otherwise you would *need* at least a bit more then the size of your RAM.

Gustavo Carneiro said...

Damn :-/

Actually I use hibernate only rarely, as I find it too slow.

But I really cannot stand the way Linux handles misbehaving applications. You give it 3 GB of swap and then it becomes trivial to write an application or file that DoS'es the system.

Tiago Cogumbreiro said...

I think there is some way to cut processes' resources, in order for this not to happen. But maybe it is just a product of my imagination ;-)

Gustavo Carneiro said...

There's ulimit. But is a per-process limit. If you limit a process's memory to 1 GB, but you already have 2 GB occupied with 1.5 GB of RAM, that extra 1 GB a new process could allocate might very well thrash the system anyway.