login
Header Space

 
 

Has swap still a use with 4 GB of memory?

June 19, 2008 - 4:56am
Submitted by Anonymous on June 19, 2008 - 4:56am.
Linux

In regard to the necessity of Linux swap almost everyone refers to this article: http://kerneltrap.org/node/3202 Only... it's 4 years old! Currently I have 4 GB of DDR2 memory in my machine and I can't imagine that I need swap. Is this true? Or does swap still has a purpose after all these years? And what about the future? When will swap be obsolete? With 8GB? Or 16GB? Or 32GB?

Re: Has swap still a use with 4 GB of memory?

June 19, 2008 - 3:00pm
Robert Brockway (not verified)

Hi. The arguments in the thread still hold - they are independent of how much memory a system has. The speed difference between ram & disk may inpact how much you want to swap however.

To my mind, there are two main reasons to continue using swap:

1. Swap lets you exchange rarely used memory for commonly used memory. If you start a large app, use it for a while to get it grab some memory and then leave it running it is consuming resources. If you have no swap it will continue to use ram for as long as it is left running. This would be days, weeks or months. If you have swap the system will notice the app is not in use and will swap it out allowing for the memory to be reused as disk cache. Disk caching improves overall performance. So there is a performance hit when the app is swapped out (and potentially swapped back in) and there is a performance gain due to the use of disk cache. Except in usual usage patterns the rate of cache hits will be high enough to make this a significant win.

2. Swap protects you from the dreaded OOM Monster. The Out of Memory monster will rise from the swap and devour your poor unsuspecting applications whenever the system cannot allocate memory. If a process grabs a lot of memory then swap can be difference between a dead system and a live system. You do not want the OOM Monster to wake.

Cheers,

Rob

Oops

June 19, 2008 - 3:02pm
Robert Brockway (not verified)

Ok I was supposed to be making a joke about the monster rising from the _swamp_ and I buggered it up :) No the OOM monster doesn't rise from the _swap_ :)

Rob

I agree with Rob, but I

June 20, 2008 - 4:51am
Martin Tournoij (not verified)

I agree with Rob, but I would like to add that it very much depends on what you do with your system.

My (FreeBSD) desktop has "only" 512MB RAM, and it doesn't really matter if I use swap or not, in my case, the only X application I use is Opera, other than this I only use fast console applications (Vim, mutt, etc).

I do have a swap partition, but my system almost never use, only on the rare occasional startup of the gimp or something, but other than this my 1GB of swap is doing nothing.
But with a 250GB disk, who cares? It will be difficulty adding swap once your disk is partitioned.

Wut?

June 20, 2008 - 8:28am
Anonymous (not verified)

1. Swap lets you exchange rarely used memory for commonly used memory. If you start a large app, use it for a while to get it grab some memory and then leave it running it is consuming resources.

I'd rather restart programs every once in a while rather than letting them gather dust. Of course it would be best if they were conservative about memory, but you can't have everything I guess.

2. Swap protects you from the dreaded OOM Monster. The Out of Memory monster will rise from the swap and devour your poor unsuspecting applications whenever the system cannot allocate memory. If a process grabs a lot of memory then swap can be difference between a dead system and a live system.

Too much swap can also mean thrashing, and that's a REAL dead system. From a one-time OOM you can recover (task gets killed, just restart it). For a program that repeatedly touched 800 MB of your available 768 ram (as an example), you'll get continuous disk access which may become so hard that everything comes to a halt.

But yes, it's advised to have at least a little swap for strange emergencies. In the order of 16-64MB.

'just restart it'

June 20, 2008 - 12:50pm

depending on what your program is doing you can lose lots of work or time if it dies and you 'just restart it'. the purpose of the machine is to let programs run undisturbed, not to stay cool. sometimes this means overloading the machine for the sake of the application.

Hmm

June 21, 2008 - 3:20am
Robert Brockway (not verified)

Hi Anon. I disagree that the presence of swap inherently invites thrashing. A system thrashes when it is in serious memory over-commit. The alternative to the thrashing is to have the OOM killer activate. I would definitely prefer to put up with a bit of temporary thrashing than to risk a box coming down due to semi-random processes being killed. It is essentially impossible for the system to guess which is the right process to kill to free memory in any given situation. Just read the last few years of comments on LKML to see the difficulty in designing a good OOM killer.

As for restarting processes periodically to clear memory - that might be ok for Firefox on the desktop but you'll get a lot of angry users if you do it to a production MySQL daemon :)

Rob

I made my choice : no swap

June 23, 2008 - 9:58am
Anonymous (not verified)

At home my 768MB machine often thrash when using swap with lots of apps opened for too long. I deactivated swap, and it's much better now : usually Firefox is the main RAM eater and gets killed anyway (I don't mind too much because the session is still here when it restarts).

The main problem I have with

June 20, 2008 - 3:10pm
Anonymous (not verified)

The main problem I have with swap is when it is not handled 'intelligently'. If I copy two 4gb DVDs to hard disk, I don't want what's currently in RAM to be obliterated by file buffering for the first DVD (esp. if it will be obliterated by the second DVD copy).

In theory swap should always be good, but it's easy to run across cases where what you're working on is paged out of RAM only to be paged back in again. It depends on your OS, settings and work profile, of course.

I've resorted to disabling swap and am just careful about ram use. Although with 8gb ram, I don't often have to think about it.

The main problem I have with

June 20, 2008 - 5:06pm
Anonymous (not verified)

The main problem I have with swap is when it is not handled 'intelligently'. If I copy two 4gb DVDs to hard disk, I don't want what's currently in RAM to be obliterated by file buffering for the first DVD (esp. if it will be obliterated by the second DVD copy).

It should not be. Caches shrink whenever a program needs memory, and only if RAM is free, caches may expand. That's the idea of caches.

But it is

June 21, 2008 - 4:13am
Amorphous (not verified)

I only recently experienced a problem with applications taking a long time to recover from idle. The changes that made the problem apparent where:

  • Energy efficient CPU + underclocked CPU & memory + fanless heatsink & power supply + temperature regulated low noise case fan + fluid dynamic bearing hard disk. This allows me to leave my desktop machine on for months at a time without any irritating noise.
  • An enormous hard disk - applications got installed with almost no consideration
  • Open office - it is a memory hog
  • PVR - I tend to record an entire series, and watch it over a weakend.
  • beagled - tries to index heroes*.mpg every day.

The solution was relatively simple: switch to abbyword and go through /etc/cron* and obliterate anything that indexes the entire hard disk.

Open office was not sinking into the swam^Hp. The memory used for the executable and libraries was used for cache while I was asleep, and reloaded when I get back.

beagled could be much improved if it understood the last-modified date field of files and directories. beagled is not the only walking disaster - the man pages only need to be re-indexed after I install/remove/upgrade software. Not every single day.

If indexers get fixed, and if there is still a problem, it might be nice to have an extra flag for the open syscall that means data read from this filedescriptor should not be cached for long. The script that indexes man pages has ignored support for checking last-modified dates for years, so I assume that if the syscall magically appeared tomorrow, it would lie around unused for another decade.

There is some hope that solid state hard disks will make swap fast enough to hide some problems, but there are a few issues to deal with first.

  • SSD's can handle many more IO ops/second than even the most expensive hard disks. This would allow people to achieve the same results with a smaller number of servers, so SSD's are not popular with server manufactureres.
  • The price of flash is falling rapidly. Distributors do not want to be caught holding stock when the next generation of devices becomes available.
  • There is still a myth about life expectancy but in real life even with unrealistic amount of writes, wear levelling ensures that SSD's outlast hard disks.
  • The sustained read/write speed is only respectable on large expensive SSD's.

the syscall you asked for...

June 21, 2008 - 4:54am

you asked for a flag [...] that means data read from this filedescriptor should not be cached for long and assumed that it would take years for this feature to get noticed by application developers.

please take a look at fadvise()/posix_fadvise() and madvise()/posix_madvise() :)

I think swap is a bad idea

June 21, 2008 - 11:50am
Anonymous (not verified)

I think swap is a bad idea on a system with lots of memory. The only way you can run out of memory whith a known workload is to encounter a memory leak. If you have swap - you system won't recover because it will try to swap for ages. Without - you get your oom killer and you can use ur system once again. If loaded binaries are not used they should be removed from memory because there is a copy on a disk. Overcommiting memory encourages monsters like azureus. Each program should get as much memory as it needs. Believe me, it is much faster for a human to decide which program to run than whaiting minutes for hundreds of megabytes to be thrown around through swap every time i switch program focus. I myself run linux without swap for around 5 years. My workload is mostly programming. I even keep temporary object files on tmpfs which is in ram without any problems. I dont run out of memory mostly because I try to pick efficent programs.

Isn't swap necessary for

June 23, 2008 - 8:10pm
Anonymous (not verified)

Isn't swap necessary for Suspend To Disk/Hibernate? And if so, how much?

I think for suspend to disk

June 24, 2008 - 8:45am
Anonymous (not verified)

I think for suspend to disk you need a storage space an large as your ram.

suspend-to-disk uses

June 24, 2008 - 3:57pm
Anonymous (not verified)

suspend-to-disk uses compressed images, so you can do with less swap than RAM (if it compresses well). It also only stores actual program images, so caches are likely to get evicted, the kernel is mostly unloaded, etc.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
speck-geostationary