Swap vs. no swap

Table of contents

IntroductionBACK TO TOC

This short article deals with simple question. How exactly lack of swap partition affects Linux’s performance. What would happen if you turn the swap off?

The obviousBACK TO TOC

Memory leaksBACK TO TOC

The obvious price one would have to pay for turning the swap off is lack of so called virtual memory. The term virtual memory refers to several different things really. What I mean is that when a program leaks memory, the memory it has leaked will remain unused as long as the program still running.

On the contrary, when swap is on, operating system copies memory that is not in use to the swap partition, by that freeing actual memory. So when one of the programs has memory leaks, memory that being lost by the program finds its way to hard drive instead of continuing to take precious memory.

I never heart of a program that claims that it has no memory leaks. Every program looses memory. It is a matter of time until it will ran out of memory and then the program will crash. Swap is not an ultimate solution for this problem because swap space can run out too, yet it significantly prologues live of the individual processes and the overall system.

HibernationBACK TO TOC

It is being referred to as hibernation and perhaps few other ways. At the bottom line this is a method to fast turn on and off the computer.

Instead of turning off all services and programs and then initializing them again, operating system suspends execution of all processes, then writes all used memory to swap space and then turns off the computer.

When you turn your computer back on, instead of starting a brand new session from scratch, operating system restores memory from swap and resumes all programs that were running, without reinitializing them.

By the way, one of the reasons why recommended swap space size is twice the size of the RAM that is in the computer is to let the operating system to accommodate all your RAM in swap partition – to allow hibernation of course.

Obviously, when you don’t have swap partition, you cannot do hibernation anymore.

The less obviousBACK TO TOC

I/O cacheBACK TO TOC

Linux uses all available memory to do I/O cache – that is to save portions of the hard disk space and to serve it straight from the memory avoiding relatively long disk access. This is why for instance when you copy several large files from one place to another several times in a row, second copy takes significantly fewer time then the first. This is also the reason why, in Linux, it takes a second to write a megabyte to a floppy disk, a device known for its slowliness – the data being written to the memory and not to an actual disk.

Also, disks optimized to read and write large chunks of data. Average program on the contrary reads and writes small chunks of data. To speed up the process, Linux collects several small read and write requests into bigger thus increasing disk performance. This however requires memory.

What many people don’t realize is that Linux uses all available memory and always tries to free even more memory for caching. When it sees a piece of memory that some program did claim, but doesn’t use very often, it will copy it to swap partition and use the actual memory for caching. Once the program will try to do something with this memory piece, Linux will detect this and read the contents of the memory piece from swap partition back to the real RAM.

By the way, this is the reason why when you minimize a program and after a while restore it, it takes it a few seconds to become fully functional – Linux had moved pieces of its memory to swap partition and now this memory has to be restored. Reading data from disk takes more time than actual memory access, thus the delay you see when you restore the program. You can change how hard the operating system tries to free memory for cache by changing value in the /proc/sys/vm/swappiness. The value varies between 0 and 100. Value of 100 means that operating system will not wait much time to move memory to cache. Note that whatever swappiness value you have, Linux tries to keep your system stable and will move pieces of memory into cache if it thinks it is necessary.

The real issue here is that this mechanism should be very balanced. Linux is optimized to use all available memory for caching. When it doesn’t have enough memory, time it takes to access data on disks will rise! Since all programs leak memory, over the time Linux will have less and less memory for caching and disk access speed will drop.

Bottom lineBACK TO TOC

Bottom line is that, without swap:

  1. Your system will be less stable.
  2. Your system will not be able to hibernate.
  3. Disk access speed in your system will be slower compared to a system that has swap partition. Moreover, disk access speed will drop in the course of time.

In case you have further questions, please email me to alexander.sandler@gmail.com.

Did you know that you can receive periodical updates with the latest articles that I write right into your email box? Alternatively, you subscribe to the RSS feed!

Want to know how? Check out
Subscribe page

34 Comments

  1. Andre Goddard Rosa says:

    “By the way, this is the reason why when you minimize a program and after a while restore it, it takes it a few seconds to become fully functional – Linux had moved pieces of its memory to swap partition and now this memory has to be restored.

    :-)

    Hopefully this behavior had an improvement on latest kernels:

    http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=8cab4754d24a0f2e05920170c845bd84472814c6

  2. Nirmal says:

    My system does not have swap space enabled. The system has 1 GB of memory and the apache process running on the system takes aorund 100MB of ‘VIRT’ memory and 1 MB of physical memory. I can see these outputs from Top.

    Now, VIRT=RES+SWAP. My Question is as I do not use swap sapce does the apache process use physical memory(RAM)?

    How does top command calculates the VIRT value when there is no swap space on system?

    Thanks.

  3. @Nirmal
    Swap by itself is not a virtual memory. It is merely one of the helpers that improve performance and change how memory leaking programs affect overall system stability. In particular, when system runs out of physical memory, it writes some unused memory areas into swap partition, freeing memory by that. On the other hand, virtual memory allows to use physically sparse areas of memory as continuous area memory.
    Virtual memory refers to physical memory + swap. Since you don’t have swap, on your system it would be physical memory = virtual memory. In terms of capacity of course.

  4. San says:

    My computer has 3GB memory. I didn’t configure any swap space for my system (Ubuntu 9.04), yet it supports hibernation quite well.

  5. @San
    I think you don’t really mean hibernation. Computer cannot hibernate without swap memory. You probably mean sleeping. The difference is that in sleep mode computer still works and consumes power while in hibernate mode it comes to complete stop.

  6. Paul van der Vlis says:

    You say a system with swap is faster and more stable, but you compare a system without swap with the same system with swap.

    What if you would compare a machine with 6 GB RAM and 2 GB swap with a machine with 8 GB of RAM and no swap ?

  7. @Paul van der Vlis
    It doesn’t make sense. Why comparing two different systems?

  8. Semi says:

    memory leakage is not an argument against having no swap.
    its just a matter of the size of the overall memory one system has
    until it breaks down.
    i.e. whether there are 8GB ram or 4GB ram and 4GB swap doesnt change
    anything. when the 8GB are eaten up, its over.
    besides with ram-only, there is no slow down caused by swapping
    until the death.

    additionally, if there is plenty of ram, there should be no reason
    for i/o cache data to kick program data into the swap area.
    just because big files are being copied doesnt mean it is acceptable
    for gui’s to be slowed down after the file operation is done.

  9. Paul van der Vlis says:

    @Alexander Sandler

    I am not comparing 2 different systems. It are 2 the same systems, with the same amount of virtual memory. The only difference is the kind of virtual memory.

    You are comparing 2 different systems. I think it is normal that a system with more virtual memory is “better” then a system with less.

    Realize that in some kind of systems it is cheaper or better to add RAM then to add swap space. E.g. in diskless systems.

  10. @Paul van der Vlis
    Your point, as it seems, is that if you have better hardware, it still does not guarantee that your system will be faster. You are right, of course. Although, you have to agree that having better hardware is likely to improve system’s performance.

  11. cwillu says:

    Your claim that there are no programs without memory leaks is factually incorrect. Even when constrained to a definition consisting of single long-lived processes, the claim is incorrect.

    Yes, some programs are sloppily written. Yes, a memory leak occasionally slips into an otherwise stable program. No, this is not the normal state of affairs.

  12. I can’t get your site thow show up correctly in k-meleon and I thought you should know, cool website though!

  13. @Computer Support
    Thanks. I’ll check this out.
    (What’s K-Meleon for heaven’s sake?)

  14. Why Linux Consumes Available Memory? | LinuxReaders says:

    […] Article 2 Linux uses all available memory to do I/O cache – that is to save portions of the hard disk space and to serve it straight from the memory avoiding relatively long disk access. This is why for instance when you copy several large files from one place to another several times in a row, second copy takes significantly fewer time then the first. This is also the reason why, in Linux, it takes a second to write a megabyte to a floppy disk, a device known for its slowliness – the data being written to the memory and not to an actual disk. […]

  15. Cormac says:

    All programs have memory leaks — are you for real?

  16. Cormac says:

    @Alexander Sandler – Could you point out the leak in the following Java code? Thanks.

    public class MemLeak {

    public static void main(String [] args){
    while(true){
    System.out.println(“I’m a memory leak…am I?”);
    Thread.sleep(5000);
    }
    }
    }

  17. user says:

    Swap is not panacea. When swap is off and system runs out of memory – it HSLTS IIMEDIATELY after all RAM is full.
    When swap is on and system runs out of memory – it became almost hooked up and unresponsive with permanent swapping process which ends up with same result HALT but LATER.
    In any case swaping did not save system from run out of memory. It just make system death more prolonged and more painful (at least for HDD).
    I believe on average desktop usage there is no reason to use swap when RAM is 4Gb or more. Suspend-to-disk is another story.
    I have 1Gb on my PC and feels comfortable without swap but ususally I mount 1Gb swap partition which is untouched by system most of the time… BTW, I use KDE and it perfectly fits in 1Gb RAM even with running FF with dozens tabs.

  18. @user
    There’s one case where swap is very useful and actually this is where it used most of the time. This is during RAM usage spikes.

  19. Dima says:

    Can somebody please explain this:

    [553]=> free -m
    total used free shared buffers cached
    Mem: 20116 2845 17271 0 12 333
    -/+ buffers/cache: 2499 17617
    Swap: 30485 5374 25111

    I got 17G of free memory, yet over 5G is swapped out, making my UI literally freeze for seconds. Why is it using swap at all with all the free memory bleeding out of its ears?

  20. @Dima
    Simple. Because kernel had a reason to swap these 5G out of memory and didn’t have a reason the return them back to memory again. The sheer fact that you have free memory, doesn’t mean kernel can bring all swap contents back to RAM. It will do so on demand.

  21. chintu says:

    Thanks for writing this post!

  22. Alex says:

    I don’t really use swap on my systems to improve stability. I’m skeptical of your claim that swap improves stability — would you rather have the OOM killer kill a few processes so you can SSH in and fix things or would you rather have it die a slow painful death by thrashing for DAYS while you can’t even SSH in?

    With that said, I recommend against disabling swap entirely as the linux kernel codepaths that involve swap are far better tested than those without. When I disable swap completely I’ve had a few oopses with backtraces in swap paths. I usually have 64 MB of swap on a system to knock off both issues.

  23. @Alex
    Well, what do you do if oom killer kills sshd?

  24. Paul says:

    I haven’t had a system with a swap partition for over ten years, and I was curious if people still think it’s a good idea. I think Alex has some good points, but I submit the following food for thought.

    First, once your total RAM is used up and your system starts swapping, you are hosed. Swap is just too damn slow, and it really can be a “slow painful death by thrashing for DAYS.” The system will be completely unresponsive, and it’s better off dead. If you don’t believe me, just write a simple program with a memory leak that will use up everything you have slowly (e.g. in a few minutes), and try to use the system once it goes to swap. Probably you will want to throw your computer out the window.

    Second, as far as I know there is no “out-of-memory” killer. What really happens is this: program X calls malloc. malloc returns NULL. program X tries to write to NULL. program x segfaults.

    But, in the above, program X might not be the program that ate up all your RAM, so effectively once your memory is all used up, it’s just the luck of the draw as to which program will crash; i.e. it’s random, unless your program actually checks all calls to malloc actually worked, which most, quite reasonably, don’t. So random processes abort, and hopefully the garbage which ate up all your RAM is one of them, and sshd/system logger/etc are not. And, no, not having a swap partition does not make your system randomly crash due to “kernel codepaths,” it does not slow your system down, and it does not make baby Jesus cry.

    Lastly I just want to agree wholeheartedly with the position that memory leaks are not the normal state of affairs and are a result of sloppy programming, and that swap is not an answer. Probably a firing squad is a better alternative….

  25. me says:

    You can hibernate Linux without a swap partition – hibernation to a file. Please see https://www.kernel.org/doc/Documentation/power/swsusp-and-swap-files.txt

  26. Remarkable issues here. I’m very happy to peer your article. Thanks so much and I’m having a look ahead to contact you.
    Will you kindly drop me a e-mail?

  27. deepak says:

    @Alexander Sandler – Hi,
    I just wanted to know in the same thread that if my system does not have a swap device enabled then though VIRT=RES+SWAP still holds good but in top command i would expect the SWAP part to be zero. But practically top shows some swapped out part for each process. I am not able to understand what part of the process it is marking as swapped out part? If there is no SWAP device then the VIRT and RES should be equal in top output as well? is the assumption correct? if yes then why top does not shows it like that? And what does non-zero SWAP means in top output?

  28. […] more info see http://www.alexonlinux.com/swap-vs-no-swap. If you need advice to choose an appropriate swap space size, refer to I have 16GB RAM. Do I need […]

  29. […] For more on this issue, see this lkml discussion and this blog post. […]

  30. Michael says:

    There are many factual errors in this blog piece. The most egregious is the claim that all programs leak memory. If that were true, we would not have the technology we do today.

    I have developed enterprise software in C, C++, and Java. I have had memory leaks in my code. Not a one of them ever made it into a production release. Ever.

    Have you ever heard of valgrind memcheck? Look into it.

    DataStax enterprise with Apache Cassandra must be run on servers with swap disabled as a standard practice. Swap only causes problems. In fact, the kernel must be carefully tuned for Cassandra to reach it’s potential.

    Disclaimer: This represents my view and not necessarily my employers.

    Thanks,

    Michael

  31. […] 문제에 대한 자세한 내용은 이 lkml 토론 및 이 블로그 게시물을 참조하십시오 […]

  32. […] For more on this issue, see this lkml discussion and this blog post. […]

Leave a Reply

Prove you are not a computer or die *