Using swap space instead of RAM can drastically slow down a pc.

So why, when I have more than enough RAM available, does my Linux system (Arch) use the swap?

Checkout my conky output below:

conky output

Also, could this be the cause of speed and system-responsiveness issues I'm having?

updated: output of free -m

$ free -m
             total       used       free     shared    buffers     cached
Mem:          1257       1004        252          0         51        778
-/+ buffers/cache:        174       1082
Swap:          502        144        357
link|improve this question

feedback

6 Answers

up vote 34 down vote accepted

It is normal for Linux systems to use some swap even if there is still RAM free. The Linux kernel will move to swap memory pages that are very seldom used (e.g., the getty instances when you only use X11, and some other inactive daemon).

Swap space usage becomes an issue only when there is not enough RAM available, and the kernel is forced to continuously move memory pages to swap and back to RAM, just to keep applications running. In this case, system monitor applications would show a lot of disk I/O activity.

For comparison, my Ubuntu 10.04 system, with two users logged in with X11 sessions both running GNOME desktop, uses ~600MB of swap and ~1GB of RAM (not counting buffers and fs cache), so I'd say that your figures for swap usage look normal.

link|improve this answer
9  
By swapping out inactive programs, you have more memory for file caching. And that speeds things up. – jmanning2k Oct 7 '10 at 17:39
feedback

This behaviour can be configured by setting the value of /proc/sys/vm/swappiness. The default value is 60, setting it to 0 means “never use swap when there is still RAM left“ and 100 is swapping out memory as soon as possible.

To change the value temporarily (lost on reboot):

sudo sysctl vm.swappiness=10

To change the value permanently: Edit the file /etc/sysctl.conf as root (e.g. sudo nano /etc/sysctl.conf) and change the line vm.swapiness=... to the desired value.

There has been some debate on whether swapping out with free memory available is good or bad, but the Ubuntu help does indeed recommend a value of 10 for Desktop systems: https://help.ubuntu.com/community/SwapFaq

link|improve this answer
9  
Note that reducing swappiness does not necessarily mean a performance or responsiveness increase. I've seen reports of increasing swappiness translating into better performance. Don't believe anything you read that doesn't include benchmarks, and check that the benchmarks use a workload similar to yours. – Gilles Oct 3 '10 at 23:05
Does this persist across reboot? I thought /proc was regenerated each boot. – HandyGandy Oct 5 '10 at 2:10
@HandyGandy: I added information to the answer how to change it permanently. – Marcel Stimberg Oct 5 '10 at 6:56
feedback

Linux starts swapping before the RAM is filled up. This is done to improve performance and responsiveness:

  • Performance is increased because sometimes RAM is better used for disk cache than to store program memory. So it's better to swap out a program that's been inactive for a while, and instead keep often-used files in cache.

  • Responsiveness is improved by swapping pages out when the system is idle, rather than when the memory is full and some program is running and requesting more RAM to complete a task.

Swapping does slow the system down, of course — but the alternative to swapping isn't not swapping, it's having more RAM or using less RAM.

link|improve this answer
So, in a sense, swap is an in-case measure? That, and the hibernate thing? – Tshepang Jan 13 '11 at 11:58
@Tshepang: Having enough swap to fit your virtual memory isn't “in case”, it's necessary (otherwise your programs will crash due to the lack of memory). – Gilles Jan 13 '11 at 19:14
That's what I meant. It's in case you run out of memory. Oh, and before they crash, there's always oom-killer (on Linux). – Tshepang Jan 13 '11 at 19:18
1  
@Tschepang: The OOM killer is the reason they crash. (Technically you could do without an OOM killer and just not be able to allocate anything, but that would have a good chance of locking up the system; the OOM killer makes it a little more likely for the admin to be able to log in and for the important processes to keep running.) – Gilles Jan 13 '11 at 19:21
feedback

From Ubunt Swap F.A.Q. that Marcel linked to

As a base minimum, it's highly recommended that the swap space should be equal to the amount of physical memory (RAM). Also, it's recommended that the swap space is twice the amount of physical memory (RAM) depending upon the amount of hard disk

I think you should increase your swap space in your system. The swap speeds up RAM memory allocation by allowing to discard already paged data.

link|improve this answer
3  
I still find this unbelievable. Why should I need 8 GB of swap for my 4GB, never-hibernating system? Do I really need 128GB of swap for my 64GB compute node? I usually allocate no more than 1GB for swap unless there is a highly-specific reason. – David Mackintosh Oct 4 '10 at 18:45
1  
It leaves more space for caching the slow-as-all-heck HDD in lightning fast RAM. (Plus, some hibernation schemes save a copy of RAM into swapspace) – Arafangion Oct 5 '10 at 13:36
1  
@David, @Jader: The swap=2*ram figure is an old chestnut that survived well after the original justification became irrelevant — now people try to find a way to justify this figure, instead of coming up with an appropriate figure for their system. See why do we need to set swap space as twice big as our physical memory?. – Gilles Nov 12 '10 at 13:05
1  
@Gilles I stick with my position because I saw once an authoritative paper on this subject that contradicts a bunch of experts that I don't know how deep their knowledge is. – Jader Dias Nov 13 '10 at 1:47
1  
If you can remember the reference, please share. – Gilles Nov 13 '10 at 10:46
feedback

Having more available memory

Like everyone said, yes swap will help you get rid of unused memory, so it can help you having more memory available.

Hibernating

But swap can also be used for hibernating which can be really useful when you have a laptop or want to save energy and put your computer and work in hibernation before leaving work. So you can have a quicker start the morning after.

Having a hibernating function is one of the main reason we still see nowadays advise to have at least the size of RAM for the swap. That way the system can put all used RAM into the swap and goes into hibernation.

Short-comings

Take care that once swapped a process data could be read in the swap even after shutdown, unless the swap was encrypted (of course).

By using encrypted swap, you cannot hibernate anymore.

link|improve this answer
feedback

A lot of modern programs are built on bloated frameworks that drag in a lot of junk you don't actually need in order to run the program. Swapping those unused pages out frees RAM for cache and programs that can actually make use of the RAM.

I speak from painful personal experience here.

Last year, I switched one of my web sites to a promising new web server framework that was built on top of Firefox. It may sound strange to build a server-side system on top of a client-focused program like Firefox, but it had some huge benefits. Firefox is very powerful, offers some really impressive internal services, and it reduces the impedance mismatch between server and client to have both running similar platforms.

But there's a downside: Firefox is big. Really big. This was a version 1.x sort of project, so they hadn't gotten around to things like removing the GUI support.[*] My site didn't need any of that, but because the VPS technology my hosting provider used didn't allow swap space, that GUI code and all the other parts of Firefox I didn't use ate real RAM. I ended up needing 512 MB RAM minimum just to run the site without it crashing due to memory exhaustion. If my VPS had some swap space, I probably could have gotten by with a 256 MB plan.

[*] Removing the GUI code from the framework may not even have been desirable, since one of the benefits of this platform was high-fidelity web scraping, because the server side framework could download web pages from another site, and you could manipulate them just as you would on the client side. Think mashups. A lot of that sort of thing would break if you can't "render" the web page into some graphical context.

By the way, this web framework is essentially dead now, so there's no point name-and-shaming it. Best to just take the broader lesson to heart: yes, swap is still useful even if you have gigs of free RAM.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.