Can you have too much swap?

10,361

Solution 1

No

At first glance you cannot have too much swap because you can see swap as a way to increase RAM. Actually it doesn't increase RAM, it just pretends to: If you have 8 GB of real RAM and a swap space of, say, 24 GB configured, then your programs can allocate and use up to 8+24=32 GB of memory which sounds good at first.

But

If you run applications that either have memory leaks or aren't really made for running with 8 GB of memory (think of video editing, for example), then these applications will start to use that swap space, and swap is slow. The more swap space is actively in use by these applications, the more the system is busy with just moving memory around to and from the disk. This will drastically slow down the overall system's responsiveness and lead to a bad user experience.

Eventually -- when swap space is exhausted -- some applications will face an out-of-memory situation and be killed by the kernel's OOM_Killer.

From wiki:

The typical OOM case in modern computers happens when the operating system is unable to create any more virtual memory, because all of its potential backing devices have been filled.

Conclusion

Hence, one drawback of having too much swap space is: the more you have, the later this OOM situation occurs and the longer you will have to suffer from a lagging and unresponsive system.

Another apparent downside of course is wasting disk space but that might not be so important nowadays.

Solution 2

There’s no direct drawback to large swap space. With the way it is managed by the kernel an increase of the amount of swap space above what is sufficient has no or a negligible impact on performance.

The only downside of “too much” swap space is that you can’t use that space for storage.

Solution 3

You won't feel any other downsides other than less space on your disk, I think that nowadays the conception of 2x the amount of ram is outdated in the majority of systems. I usually recommend to use the same size of your ram for swap memory in laptops though in order to give you the opportunity to hibernate your pc without losing any data due to the volatile nature of RAM.

The only cases where I would consider using more than 1x the size of RAM for the swap partition is when you think you are going to upgrade your RAM size in a short time. Or if you plan to use a system where you will store in a sort of cache the amount of memory used by programs not used at the very same moment.

Hope my point was clear enough, and I repeat I don't think you will experience any problem for having too much swap memory.

Have a nice day!

Solution 4

How much SWAP does hibernation really need?

It's a misconception that you need RAM x 2 for SWAP size when you use hibernation. The swap size needs to be the size of used RAM not Installed RAM. Generally swap size needs to be 2/5th of installed RAM. To find out the bare-minimum amount of RAM needed for use:

$ cat /sys/power/image_size
3153907712

On this 8 GB RAM machine 3 GB minimum is needed to hibernate.

You can tweak the values in image_size for a smaller swap size with risk of failure. You can tweak it for a larger swap size and possibly speeding up the hibernation speed.

Reference: https://wiki.archlinux.org/index.php/Power_management/Suspend_and_hibernate


16 MB RAM in 1995, different rules for 16 GB RAM in 2018

In the days when you had 16 MB Ram, x 2 for 32 MB swap on your 720 MB hard drive made sense. A little RAM and HDD history in this 1995 Washington Post article.

23 years later some technical articles from that 1995 (although I didn't find any) might be found to mislead new users. I did however find a 2007 article recommending SWAP = RAM x 2.

Back in 1995 32 MB Swap out of 760 MB HDD was 4% of HDD. Indeed the swap partition may have been used a lot in 1995. Today in 2018, 16 GB RAM x 2 for 32 GB SWAP on your 256 GB SSD doesn't make the same sense as it is taking 13% of SSD. Today my 8 GB SWAP partition isn't being used at all unless I force it to fill it up when testing OOM-Killer: Google Chrome will take up my memory to the point where it causes my computer to freeze to a near halt. What can I do to prevent this?.

Solution 5

What is SWAP:

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

Swap should equal 2x physical RAM.


Advantages:

Provides overflow space when your memory fills up completely Can move rarely-needed items away from your high-speed memory Allows you to hibernate

Disadvantages:

Takes up space on your hard drive as SWAP partitions do not resize dynamically Can increase wear and tear to your hard drive Does not necessarily improve performance (see below)


When SWAP Partitions "Don’t Help" as in "not worthy comparing to extra storage" :

If your Harddrive has only 5400 RPM and you have little RAM lets say > 2GB. Why ? Because the system constantly wanted to access the SWAP partition, it will eventually become very slow. Even though you now have space in the memory, everything in the SWAP partition need to be moved back over. Because the system will go slow, allot stays in the SWAP partition. This can only be fixed with a reboot. Which will take a while anyway because the system need to remove everything from the SWAP partition before shutting down.


**Conclussion: **

If you would like to be able to hibernate your computer, then you should have a SWAP partition. The size of this partition should be the size of your installed memory, plus an additional 10-25% to leave room for any items that were already moved over into the SWAP partition.

If you just want a small performance boost (and you have at least a 7200rpm hard drive), then you can add a SWAP partition if you want, but it’s not needed unless you have less than 4GB of installed memory. The size of this can be whatever you’d like. However I recommend 2x the RAM as a pinpoint. IF you have enoug storage space.

If you have a 5400rpm hard drive, then you shouldn’t create a SWAP partition simply because the bottleneck will make your computer worse off. However, if you absolutely want to have SWAP, then you can still create a partition using the same sizing guidelines outlined above – but change the swappiness value to something much lower.



My OPINION:

However in any case if you use Ubuntu as your Main OS for daily use I recommend 2x the size of the RAM. Because you don't install Ubuntu just because you have a old computer. But because you want to use the system as your Main OS.

Rather buy some extra hardware if needed instead of adjusting the system partitions to keep it running. If you buy a game you also make sure your system is "up to date" instead of adjusting the settings to make it "Playable".

You can better have some extra space, SWAP, speed, power instead of having too short or need to resize everything later on. Because you need SWAP or space, bought RAM? Or need to buy ram fast because one memory slot or stick broke.

Share:
10,361

Related videos on Youtube

An0n
Author by

An0n

Updated on September 18, 2022

Comments

  • An0n
    An0n over 1 year

    I was just wondering if you can have a swap partition that is too big. If yes, when is a swap partition too big? What are the downsides/ill-effects of having a swap partition that's too big (even if I have plenty of disk space)?

    If no, what are the benefits of having more than the recommended swap space?

    • PerlDuck
      PerlDuck about 6 years
      The apparent downside is that you waste disk space. OTOH: if you have "too much" swap (say 30GB on a 4GB system), then misbehaving apps will get an out-of-memory far later and that will slow down your system. Orderly behaving apps (without memory leaks) will simply not use it. (I admit, this is a simplified view.)
    • Panther
      Panther about 6 years
      I agree with @PerlDuck - "too much swap" just uses disc space. You system will be slow if it starts using swap , in that case either run less apps, lighter weight apps, or get more RAM
    • An0n
      An0n about 6 years
      I know, so if you have enough RAM, other then using too much diskspace are there any downsides ? Except offcourse "misbehaving" apps as PerlDuck said.
    • WinEunuuchs2Unix
      WinEunuuchs2Unix about 6 years
      VTD All the questions in the Edit justifies changing close reason from duplicate to off topic as too broad. OP is obviously miffed but meta or a chat room is a better place for discussion.
    • Eliah Kagan
      Eliah Kagan about 6 years
      In case people are unsure of what @PerlDuck (and Panther) are talking about, note that even if you had effectively unlimited disk space, having more space to swap to will make processes that ought to just be terminated, and which otherwise would be swiftly terminated automatically, slow the system down to a crawl for a long time first. PerlDuck's comment would, I think, also work as an answer. If we end up reopening this question, perhaps an answer can be added about the drawbacks of having far more swap than necessary. Because there are drawbacks.
    • FreeSoftwareServers
      FreeSoftwareServers about 6 years
      I think this question would be more interesting if it's specified having low amount of RAM. If you have over 16gbs then theres no need for lots of swap and too much is simply a waste of disk space, but no harm.
    • An0n
      An0n about 6 years
      This question doesn't even involved RAM. The interesting parts are the RAM answers.
  • Panther
    Panther about 6 years
    I do not use swap at all if RAM > 2-4 Gb on a single user desktop.
  • An0n
    An0n about 6 years
    I don't see that as a downside to be honest. Since you use that space for swapping.
  • David Foerster
    David Foerster about 6 years
    @An0n: I think it would be a pretty big downside to allocate all the ~6 TB currently connected to my machine as swap space. Where would I put all my music…?
  • An0n
    An0n about 6 years
    Depends if you can boot or not.
  • David Foerster
    David Foerster about 6 years
    @An0n: What good is a computer if all it can do is boot and then not store any useful data? That sounds more like an expensive space heater.
  • An0n
    An0n about 6 years
    What good is usefull data if you can't boot ?
  • David Foerster
    David Foerster about 6 years
    @An0n: Sure, I assumed as much but you're missing the point. I think we can agree that to be useful a computer must be able to reach a usable system state (incl. being able to boot) and have access to storage that the user can use for her personal data. That requires that it be not occupied by swap space.
  • An0n
    An0n about 6 years
    @Panther Why not ? And its not about how or when you use it. Its about the size and the usage.
  • An0n
    An0n about 6 years
    I think you miss my point. Since usable system state is relative. Depends on what you are planning to do with it. In system setup you define system space and storage space. If you need extra storage, add it, this question is about the system setup in the current state. Not about upgrading soon or how much storage do I need.
  • David Foerster
    David Foerster about 6 years
    Of course it's relative. Just like "too much" is relative. If I have a bootable system and I allocate all my free space as swap space then it's no longer useful to me because of the lack of free space until I change its system configuration in a way that ends in more free space. In that sense the swap space would be "too much" during that time renders the system useless during that time. Anyway, I'm not here to argue semantics of words with you.
  • Zanna
    Zanna about 6 years
    @An0n To underline the point here, swap space is space you can't use for anything else. Disk storage space costs money. If your disk space is limited (mine is only 32GB), you want to keep as much of it as possible free to install programs and write files with your stuff in them, like music. People generally try to keep swap to a minimum for that reason, and no other reason. Swap is something the system might need to keep running efficiently, that is otherwise of no value to the user. In general, we want our system to use only the necessary resources, and leave us plenty to play with.
  • An0n
    An0n about 6 years
    @Zanna: Totally agreed. But the question is about when you have plenty of space. And use your Ubuntu as Default OS, how to set it up as good as possible AND that case can you have too much SWAP? For example when you have 1TB HDD.
  • Elder Geek
    Elder Geek about 6 years
    +1 for the consideration of the edge case. Personally, I'm not a big swap fan as hibernation is for bears and my systems are kept far too busy for that. ;-) Cheers.
  • Elder Geek
    Elder Geek about 6 years
    +1 for pointing out the misconception of the often touted 2x rule which is total bunk.
  • Elder Geek
    Elder Geek about 6 years
    Have you considered the impact of memory leaks?
  • PerlDuck
    PerlDuck about 6 years
    @ElderGeek LOL. Actually Eliah Kagan made me turn my simple comment into an answer. He convinced me that the aforementioned downside of having too much swap is a crucial thing and might be important to others. I'm with you and have little swap configured (2GB with 8GB RAM) and it almost never gets touched.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 6 years
    @ElderGeek Thank you. I added a history section on when 16 MB RAM times 2 for 32 MB SWAP may have made sense.
  • David Foerster
    David Foerster about 6 years
    @An0n: "Plenty" is still far from "infinitely". As long as the available amount of a resource is finite there will always be a point at which over-allocation to one purpose will lead to under-allocation to another to a point where parts of the surrounding system cease to function efficiently and as intended/desired.
  • zurg
    zurg about 6 years
    @ElderGeek Hi, no i did not consider it. I like your answer, i didn't know about memory leaks problem!
  • Elder Geek
    Elder Geek about 6 years
    I have had similar results with similar settings, I wish the 2x swap rumor would finally die.
  • NZ Dev
    NZ Dev over 5 years
    Another thing to consider is SSD versus HDD. Do we want plenty of read/writes on SSD bringing it quicker to failure in that part of the SSD (or is this a myth)? On my system 32GB ram I used 2GB of velociraptor Drive for SWAP but with no intention for hibernation, still not sure if this is ok, possibly I could have had no swap??? But if I was hibernating maybe 4GB would be enough, but chances are I would be using 20GB of browser, and then 4GB would not be enough???
  • NZ Dev
    NZ Dev over 5 years
    Re: your Conclusion It just does not make sense, or another way to put it is where do we draw the line. eg RAM 8GB plus 16GB swap: What if I use the 8GB of RAM and 9GB of swap, then there is still not enough room to hibernate. So what gives? In other words no matter what size you make it, you can still use it all and more. So why is Swap file not given 2 dedicated spaces 1 as Virtual memory and the other as hibernation space which would be the sum or the other 2. It just does not stack up for me and no one has ever answered this question.
  • NZ Dev
    NZ Dev over 5 years
    Sorry, your last statement/sentence is confusing, otherwise a good add-on to the above.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 5 years
    @NZDev I have my swap on SSD but it is seldom used. Last time I checked it was still 100% life left after 1 year of use. To see percentage of life left: askubuntu.com/questions/1038701/how-do-i-check-system-health‌​/…
  • Ismael Luceno
    Ismael Luceno about 5 years
    @NZDev is it better now?
  • qdinar
    qdinar over 3 years
    40 gb swap partition was made by some mistake or bug, without intention, on 240 gb ssd device, by me, on a client's machine with 2 gb ram. rest is "/". think, there is no other constant storage. as i know, it is harmful to fill ssd very much, so, this big ssd may be useful as it can prevent ssd from filling. but i doubt, is not free swap space sparsely written by some little pieces of data?
  • qdinar
    qdinar over 3 years
    after reading answers at askubuntu.com/questions/846163/… i think it is ok.