Home > Linux > Linux: Is Swap Necessary?Linux: Is Swap Necessary? Submitted by --_==Josh==_-- 2004-05-29 Linux 47 CommentsFollowing recent discussions about tuning swapiness and dynamically allocating swap as needed, a new discussion ensued on the lkml questioning the need for swap altogether when a system has “sufficient” ram.About The Author Eugenia LoliEx-programmer, ex-editor in chief at OSNews.com, now a visual artist/filmmaker.Follow me on Twitter @EugeniaLoli 47 Comments 2004-05-29 5:27 am Depends on what you do with your box.When will these FAQ die? 2004-05-29 5:27 am Look closely at this quote:Physical memory will always get full because the OS will always keep copies of file data in memory just in case it needs them again. Because new pages are always being read in and processes are always allocating new memory, the OS will have to make a decision of what pages to evict from physical memory. If a page is dirty, it can only be evicted if there’s swap. So if you have dirty pages that are very rarely used, swap allows you to keep more hot, clean pages in memory.He is precisely right. You learn this in Operating Systems Concepts books. Basically, since Operating Systems have to work around the general slowness of I/O (i.e. hard disk access), memory is used as much as possible for proper caching. So, though it seems like 1GB of RAM will never fill up, it actually will be used by the OS to cache your filesystem as much as it can. It’s true that if it fills a large portion of your memory, the cache hit ratio will inevitably drop, but the user will nonetheless experience performance boosts.I think it is a general myth (stemming perhaps from Mac OS 9) that swap is unnecessary. It has been proven to be theoretically necessary as well as practically necessary. The answer is not to eliminate swap space–it is to make virtual memory management fine tuneable so that a DESKTOP user’s experience is optimized (as opposed to “large batch jobs” being optimized, as is the case now, I think, in Linux). As another poster says, interaction is more important than large jobs finishing quicker. This is clear. 2004-05-29 5:47 am But couldn’t, say, on a system with 1 gig of RAM, 768 megs of RAM be treated as physical RAM and the other 256 be used as the swap space instead of using space on the hard disk for the same purpose? I know no OS does this now, but why not? 2004-05-29 5:58 am Because that would be wasting RAM that could otherwise be used.Think of it as storing stuff in a bag, and in a box across the room. The stuff in the box you don’t need as much as the stuff you’ve got in the bag. In this case, the bag is RAM and the box is your HDD.So lets say you’ve got CD’s you don’t care to carry around the room, BUT you’ve got this book and water and some Zoloft (…what?), and your PDA; you can carry those and access them ASAP, but what’s in the box is the stuff you don’t need RIGHT THEN; BUT you can walk back to your box and grab what CD you need.$ 2004-05-29 6:06 am Without a swap file and only 1G ram then how can you work on a 2G file? Swap files allow you to extend (if even slower than HD access) ram to allow programs that require contigious file access. 2004-05-29 6:06 am Linux runs fine with swap turned off, as long as you have enough physical RAM. On the average Linux/KDE system with 256Mb of physical RAM, doing some web browsing and email editing under KDE leaves swap untouched.The point is that turning swap off gains you nothing, but turning swap on gains you _something_ when the size of your working set exceeds the amount of RAM you have installed.This _something_ depends on how well tuned your swap system is. 2004-05-29 7:42 am I think the benefits derived from the use of swap space have to do mostly with having contiguous blocks of memory available for programs that may need to allocate memory in this manner. An example that comes to mind is large databases, but I could be mistaken since I dont have much experience with these.However, using KDE with some 5 odd applications (one of them mozilla, the often-blamed program for consuming indecent amounts of memory) running, the system monitor shows “0%” usage of swap memory used, which seems to suggest that at that amount of load the system is strictly using only physical RAM. I haven’t tweaked or played around with the swap settings at all, so the result may be typical for most *desktop* users I suppose. 2004-05-29 7:50 am File caching seems to be fine to me, but the question should be “what files should it cache?”. Here, when I copy a DIVX to another medium, it fills me RAM completely. ‘free’ will report that I have over 600MB ‘cached’. When I restore my minimized Firefox, I will have to wait because all of this ‘unimportant’ data has been cached.Same is true for ‘updatedb’ and the like. There is no point in trying to cache everything. Just programs, libraries and maybe config files.Is there no way to tell Linux 2.6.x to avoid caching at least huge files? 2004-05-29 7:52 am Err,” When I restore my minimized Firefox, I will have to wait because all of this ‘unimportant’ data has been cached. ”should be:” When I restore my minimized Firefox, I will have to wait because all of this ‘unimportant’ data has been swapped. “ 2004-05-29 9:20 am yes 2004-05-29 9:33 am yes, most distros expect a swap partition to be defined at install time, however, later this can be changed to loads of swap files, and the original swap partition can be deleted and reformatted as something else.I have never tested this for performance myself, but will someday. 2004-05-29 10:27 am Quote:Without a swap file and only 1G ram then how can you work on a 2G file?Well, it’s a good practice to not load such large files in memory at once. Most programs do it anyway, and they become extremely slow.Best practice is to stream them from the hd.Only read those parts from the hd that are on the screen.Then a 2GB file suddenly becomes a 20KB file. 2004-05-29 10:49 am To me, it appears rather simple to me. If an application wants more memory, but none is available, there are two possible scenarios:– You have swap. Some unused application gets swapped out.– You don’t have swap. Parts of the file cache get discarded.So I would say, there are two possible choices:– You want your PC to be always responsive (but don’t mind slower disk access) -> no swap.– You want the fastest possible disk access (but don’t mind applications being swapped out) -> swap.So, I would say: swap isn’t necessary. Unless you want to edit that 19200 dpi scanned photo with GIMP in 32 MB of ram, that is. 2004-05-29 11:15 am I think we should be able to set the priority of swappin processes much like the cpu scheduler does for .. cpu power, with nice.On a desktop you could set X/your wm to almost never swap, while background processes would still swap, and hey, if you go out of physical ram and they swap, too bad for their performance, because you, the user, you don’t care much. You care about reactivity. You care that when you click a window, it goes right away to you. That enhance pleasure of use and productivity more than the small perfs you might loose (when the vm doesn’t swap wrong stuff…) by swapping. 2004-05-29 11:46 am Hmm… at one point, they’re saying that a full file cache will “feel exactly like swapping”.I wonder if there’s a clash here? What if the swapping/caching was integrated into a single queue, so that it all gets managed well? That would probably help, except in cases where swap and files are on separate physical disks or platters.Of course.. maybe this is already being done. I don’t know. 2004-05-29 11:55 am As others have mentioned, I think it would be useful to mark certain things as important to keep in memory. User-interface stuff like the (truly) core librarys like libc, shells, the core gui toolkits of desktops, window managers, the core of X and perhaps the main (window-drawing) threads of some apps. Particularly nice would be a resident getty process and consoles, so that, if something is going wrong, you can quickly login in on a new session and take control. 2004-05-29 12:08 pm Linux runs fine with swap turned off, as long as you have enough physical RAM. On the average Linux/KDE system with 256Mb of physical RAM, doing some web browsing and email editing under KDE leaves swap untouched.I’ve forgotten to mount my swap partition in the past, and the system has quickly fallen through the floor. 2004-05-29 12:15 pm “I think we should be able to set the priority of swappin processes much like the cpu scheduler does for .. cpu power, with nice. ”Not exactly a priority, but there is the swapiness value, between 0 and 100, somewhere in /proc/vm (the file name is exactly swapiness). 2004-05-29 12:18 pm “As others have mentioned, I think it would be useful to mark certain things as important to keep in memory”You can, there is the whole mlock scheme, but it is very strong, as it is never put out of the physical memory. It is for example used when you cannot bear the cost of a page fault (real time issue). 2004-05-29 12:57 pm I understand that Linux uses all of the available RAM to make things the user has done before quicker the next time they do them.But if I open a GNOME app, launching it isn’t any quicker, so what happened to all my use up RAM? I don’t understand it.KDE has a performance option now for launching apps, isn’t that a hack.I just don’t understand why when I load a program it quickly gets unloaded from RAM.Shouldn’t these DE’s be designed to always have certain libraries always loaded. The only way I guess is to leave apps open all the time.Pre-linking does nothing for 450Mhz machines.I know this sounds like a RANT, but this is a genuine question of mine. This always goes through my mind while using linux. My only joy with linux is using Mplayer. It is amazing how I can play 1300k XVID smoothly.Currently using FC1, next week or two I will install FC2, lets see if anything is any better. 2004-05-29 1:24 pm “Not exactly a priority, but there is the swapiness value, between 0 and 100, somewhere in /proc/vm (the file name is exactly swapiness).”The file is /proc/sys/vm/swappiness. 2004-05-29 1:44 pm Is Con Kovilas patch in the kernel included in FC2?This is what I am talking about, it sounds perfect for desktop users.http://kerneltrap.org/node/view/1044 2004-05-29 1:44 pm isn’t /proc/sys/vm/swappiness global ?Would be nice to have a swappiness per process 2004-05-29 1:47 pm Hmm don’t get too excited about what I posted. Have a look here and the date it was posted.http://kerneltrap.org/node/view/3000 2004-05-29 2:34 pm i just can’t find it in /proc/sys/vm:S 2004-05-29 3:41 pm I don’t know how swap under Linux works, but there should be cases when turning it off should be favorable:Imagine a case where I have a fair amount of RAM, more than enough for hosting all the used parts of the OS as well as my applications and working files (say word processing documents, emails, cached internet pages. This might well fit into 512 MB and I still have some MB free. Just to use some figures, imagine all my working set would fit into 450 MB RAM, including all the cached file system structure and directories and everything. I know I will close the browser or the word processor now and then and re-open it later, and I know as well I won’t use GIMP or recompile the kernel with gcc. I also know I won’t access tons of files on my HD but keep working on the same ones all over again.Unfortunately, there’s no way for the OS (or the swap manager, to be more precise) to know what I’m going to do. So as I’m getting closer to the 512 MB limit it will start paging out parts of my applications, OS or data that hasn’t been accessed for a while. Why does it so so? It has to take into account that I might want to start another program like gcc. And in that case I wouldn’t want to wait a) for the system to page out a lot of memory to make room for gcc b) load gcc into memory c) page out even more unused data to make room for the gcc compiling working set. So what Windows would do (don’t know about Linux) is to start swapping out memory as soon as (don’t know the exact figures) 66-75% or my physical memory is being used just to make sure it can still load another program without first having to swap out huge amounts of memory. Of course this will always be a compromise, because when I suddenly want to run my huge database with a memory footprint of 300 MB all the effort would be more or less in vain. But in standard scenarios this swapping behavior would be pretty clever.But in my case this means it would start swapping out memory as soon as 340 MB are being used and try very hard to keep the amount below 384 MB. In my example case above, there would be roughly 70-110 MB RAM to swap out. So after I’ve been working on my docs for a while, it would say swap out most of my email program. Now imagine I decide to take a break and write a short email. This forces the OS to page in those say 80 MB again which of course slows down the process considerably.And now please remember what I’d said above: With my limited utilization of available programs and system resources all the swapping is unnecessary since I will never use any other than those programs that I’ve initially fired up. So in this scenario swapping would be counter productive in every means.So in my view there are scenarios when swapping does not make any sense. They might be pretty rare and limited to more or less single use systems, but depending on the available RAM there might even be cases where even more complete utilization of available programs would still fit into RAM and thus make swapping counter productive.Anyway, if you can prove me wrong I’m perfectly willing to admit that swapping always makes sense, but so far none of your arguments above can convince me.btw, working with a 2 GB file (like a huge database, a movie or whatever) does NOT mean I need 2 GB of (physical or swap) memory. No program ever that is meant to work on files like that needs to have them in memory. Even files on disk are sort of random access and working with a database only requires to have one particular entry in memory (or not even that). And furthermore, loading the huge database into memory and then swapping it out wouldn’t give any advantage at all because now the system would have to random access the swap file instead of the database file which is in means of used system resources like HD access exactly the same. This is why no database utilizes swap memory.Philotech 2004-05-29 3:48 pm “Basically, since Operating Systems have to work around the general slowness of I/O (i.e. hard disk access), memory is used as much as possible for proper caching. So, though it seems like 1GB of RAM will never fill up, it actually will be used by the OS to cache your filesystem as much as it can.”No it won’t. It depends on the filesystem. It makes more sense to set an amount of RAM to be used for the disk cache at startup, assigning suitable amounts for each partition.Programs that handle very large files (video and audio editors) normally leave almost all of the file on disk, loading only the section being displayed into RAM.The big objection to swap is that it is incredibly slow. I don’t think it is needed if you have 1 Gig of RAM. 2004-05-29 4:14 pm No it isn’t necessary. Thats why you have the option of not adding swap to /etc/fstab. 2004-05-29 4:24 pm “i just can’t find it in /proc/sys/vm”Are you running a 2.6 kernel? 2004-05-29 5:25 pm Sadly, it is possible for one user to consume more than one gigabyte of RAM on a single user system. I do it every day at work. 1 gigabyte doesn’t seem that roomy to me — I have 1 gigabyte but I consume around 1.3 gigabytes. Thank you, swap.I am willing to stipulate, though, that if I had a terabyte of RAM, I would probably turn swap off. 2004-05-29 6:24 pm why does swap have to be a physical partition, and not, a file? for that matter, why can’t it be a semi-dynamic sized file? 2004-05-29 6:54 pm why does swap have to be a physical partition, and not, a file? for that matter, why can’t it be a semi-dynamic sized file?Linux can use both as swap space, but to my knowledge, it’s currently beyond it’s ability to dynamically manage the size of that swap space. 2004-05-29 7:26 pm why does swap have to be a physical partition, and not, a file? for that matter, why can’t it be a semi-dynamic sized file?Linux can use both as swap space, but to my knowledge, it’s currently beyond it’s ability to dynamically manage the size of that swap space.Ah, a Windows or Mac Classic user. While it is possible as Windows and Mac demonstrate, it’s also more complex and hence, slower. Why take the chance of adding bugs and bloated, slow code to your VM? Just use a separate partition and don’t worry about it.That said, I prefer a non-swapped system. I’ve worked on both swapped and non-swapped systems, and I prefer the former. If I’m using so much memory that the program might error out, I’d prefer to know it so I can go buy another stick rather than cuss about how slow the system is. Why waste space on my HD for swap when I can put it to more productive use? Swap isn’t faster, just “safer” for people who operate near the edge of total RAM usage. As someone else said, swap was only added as a cheap way to extend memory back when RAM was expensive. RAM is cheap now – buy some. If you only have 1G of RAM but need 2G for what you are doing, buy another 1G. You’ll be a LOT happier than sticking to 1G and trying to use the HD as the other 1G. 2004-05-29 7:31 pm I meant the latter above… I prefer non-swapped systems. Sorry about that! 2004-05-29 7:50 pm 1st off, I hate Swap/VirtualMemory/PageFile with an absoulte passion. The true usefulness extends only to systems with limited RAM. Thats fine, but if you have a system with a large quantity of RAM, your needs for Swap is negilible or limited depending on your uses. Imagine what WinFS or Gnome Storage is going to do to your Hard Drive, the device that stores your information.Virtual Memory/Swap today has basicly 2 options; ON/OFF. Why not add a third option; Limited ON. This 3rd option is basiclly limits access to the swap on a per app basis. For example; letting your video editor, dvd editor, or image editor(Photoshop/Gimp) access to the swap while making your Browser, Email, and Messanger programs not use the swap.This is a simplistic viewpoint on a complex technical issue. 2004-05-29 8:50 pm I have two machines, a desktop and a laptop, both have 512MB ram. The laptop has never had a swap partition. I’ve been using it like that for about 6 months, without any problems. After I read the KernelTrap article, I wanted to check how large the swap partition on the desktop is. It turns out the swap partition was on a disk that died several months ago… I guess the fact that I didn’t notice until now really shows that _I_ don’t need swap Other people may need more memory than me. Do these people need swap? I don’t think so. If you need 1GB, should you have 512MB physical ram and 512MB swap? Why not just buy the extra 512MB? It’s only about $100… The same thing applies if you need 2GB. If you _need_ it, you (or your employer) can probably afford it. 2004-05-29 8:58 pm Swap files in kernel 2.6 should be as fast as swap partitions. 2004-05-29 9:41 pm For a while once, I ran Linux without swap because I totally forgot to put it in fstab… I re-enabled it but I haven’t noticed a difference (I have 512MB RAM), and KDE Info Center (cool app) always reports swap unused. I did once run into a problem without swap on a huge compile (OpenOffice I think, which I gave up after two days and downloaded the binary, those were the days with Gentoo).Personally I am not a big fan of swap and would prefer programs to be more clever with memory usage. Something about using the disk to cache the disk sounds a bit stupid to me. You don’t need to use hardware paging to deal with data larger than your memory… and can’t for data larger than your address space. 2004-05-29 9:45 pm Swap files in kernel 2.6 should be as fast as swap partitions.There is no good reason they shouldn’t be, providing the file is statically sized and has contiguously allocated blocks. I might look in to this, I prefer the convenience of not needing a seperate partition. 2004-05-29 9:56 pm I’m not an expert but ….One thing should be clear:(A) 768 MB Physical Ram + 256 MB in ram swapshould be faster than(B) 768 MB Physical Ram + 256 MB on disk swapif the kernel had the capability of making a ram disk swap (??).What about the cases(C) 1GB Ram with no swap(D) 1GB Ram with 256MB on disk swap.(E) 1GB Ram with 512MB on disk swap.(F) 1GB Ram with 1GB on disk swap?(C) should be better than (A) because more physical memory is available as somebody stated above. Furthermore, (A) and (C) have an upper limit of 1GB so in this regard they are equivalent.Is (D) better than (C)? It’s clear that (D) is safer than (C) in the case when the system requires more than 1GB. It’s questionable whether (D) makes the system feel faster than (C). For example, if the total process memory never exceeds more than 256MB, then the remaining 756 MB can be used for file cache in (C). In (D), since the swap exists, the system could conceivably swap out 128 MB of process memory in order to cache more files. This could cause the system to feel slower if the extra 128 MB of file cache are not needed after they were accessed for the first time.What’s not as clear is how do (D), (E) and (F) compare.That would require benchmarks and a detailed understanding of the VM.By the way, maybe somebody knows the answers to the following:(1) Is there a way to set the maximum amount of physical ram used for file cache?(2) Is there a way to limit the amount of virtual memory a user id can use. If not, has it been considered?(3) Likewise, is there a way for the OS to place a limit on how much virtual memory a process can use? If not, is this feature being considered. 2004-05-29 10:32 pm I have 512MB RAM and a 1GB Swap partition. Generally I don’t use the swap, but occasionally it will use a few MB’s. Sometimes I do deal with large amounts of images and you will see the swap in use. The most I’ve used is around 600MB. The system is surprisingly stable and quick, although you can definitely tell what’s been put in swap and what’s still in memory.The kernel does seem to keep the right things in memory and put the right things into swap though. I’m pretty impressed. That was kernel 2.4, since (recently) moving to 2.6 I haven’t made such use of my memory.At first I actually expiramented with a swap file, it was obnoxious to setup as an inexperienced user.Swap as a regularly used major thing would be very annoying though. I think a large swap partition is a waste for most. But a small one is definitely a good idea. I have a large one because my usage varies a lot and I don’t have money for more RAM right now. 2004-05-30 2:29 am Virtual memory is not neccessary on systems where developers pride themselves in writing slick unbloated software. I used to run multiple apps on my Amiga with 4 megs ram and rarely had to close anything to free up some memory, I didn’t have to resort to shuffling apps either. There was no need for caching, apps always loaded up quickly and that was back in the days of relatively poor hard disk performance. 2004-05-30 6:26 am Using Archlinux for about a month now (and very happy with it I might add), I have noticed the mosr tam I have used is about 60k as reported by TOP; gtkrellm seems to agree graphically that I use nearly no swap; mind you I have 512 of slow ram (PC 133). I was thinking about going swapless; but I don’t see the effort just to regain my 256 megs (I was lazy on set up with arch; I just let it default), and am very happy I did no make the 512 partition that I wanted to. 2004-05-30 9:50 am “Virtual memory is not neccessary on systems where developers pride themselves in writing slick unbloated software. I used to run multiple apps on my Amiga with 4 megs ram and rarely had to close anything to free up some memory, I didn’t have to resort to shuffling apps either. ”Keeping the programs small and efficient does help, but it is really the data that needs the space. You can’t do image processing on a 50Meg image in 4 Megs of RAM, except by using a program that keeps the file on disk, and this is very slow. Try Image Studio.The programs that really eat RAM are those that generate a new copy of the data after each processing step, such as Image Engineer and SoundFX. This is great as it allows perfect Undo; but it does need plenty of RAM. For instance, you can keep ten steps of a 50Meg image in 512 Megs. After that, you have to close some of the instances.However, 2 Gigs will allow you to keep forty stages. I think we are heading toward having enough RAM for swap to be hardly ever needed except by one or two special programs.Now that everyone has high resolution scanners, big image files are more common. 2004-05-30 2:19 pm Just tried turning off swap on my p166 24Mb ram laptop.It would be handy for me as swapping less would enable the hard drive to spin down more often, and extend battery life.In console mode it works fine, I can run a number of consoles and apps without trouble. Playing MP3s at the same time is fine too, and if I precache 8mb or so of mp3, the hard drive spins down, and stays stopped for quite a while.In X running fluxbox I can start smaller apps ok, but running PD (the main use of this laptop) is a no go. It refuses to start saying ‘failed syscall’ or something.Still, it’s interesting to see what happens with no swap and very little memory. 2004-05-31 8:35 am It’s one of these things that you’ll not miss until you need it though. So if having it on isn’t causing problems, its probably just as well leaving it on.Finer grained control of what apps use swap space and when would probably allow the best of both worlds, but would probably be quite complex. 2004-06-01 1:00 pm Ever since the dawn of computers one universal truth has been known across all platforms…..One can never have enough memory!I dont care if you a 4 gigs of RAM….Swap is important.