23

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?

7
  • 5
    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.)
    – PerlDuck
    Mar 18, 2018 at 15:45
  • 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
    – Panther
    Mar 18, 2018 at 15:49
  • 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.
    – An0n
    Mar 18, 2018 at 15:50
  • 1
    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. Mar 24, 2018 at 2:35
  • 3
    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. Mar 26, 2018 at 13:04

6 Answers 6

9

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.

4
  • +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
    Apr 8, 2018 at 16:05
  • @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.
    – PerlDuck
    Apr 8, 2018 at 16:23
  • I have had similar results with similar settings, I wish the 2x swap rumor would finally die.
    – Elder Geek
    Apr 10, 2018 at 20:08
  • Well keep a blunt eye on it, nothing should be leaking. Also if you are OOM at 16GB combined, another 4GB may help out. Firing up 4 IDEA servers, a local database, and Teams on a laptop is just asking for it.
    – mckenzm
    May 10, 2022 at 5:52
7

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.

11
  • 1
    @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…? Mar 19, 2018 at 22:25
  • 2
    @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. Mar 19, 2018 at 22:30
  • 2
    @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. Mar 19, 2018 at 22:40
  • 2
    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. Mar 19, 2018 at 23:26
  • 1
    @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.
    – Zanna
    Mar 24, 2018 at 15:48
4

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!

4
  • 2
    I do not use swap at all if RAM > 2-4 Gb on a single user desktop.
    – Panther
    Mar 18, 2018 at 19:00
  • 1
    @Panther Why not ? And its not about how or when you use it. Its about the size and the usage.
    – An0n
    Mar 19, 2018 at 23:01
  • 2
    Have you considered the impact of memory leaks?
    – Elder Geek
    Apr 8, 2018 at 16:13
  • @ElderGeek Hi, no i did not consider it. I like your answer, i didn't know about memory leaks problem!
    – zurg
    Apr 10, 2018 at 17:02
2

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.

1
  • 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. Jan 29, 2019 at 3:07
2

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?.

4
  • +1 for pointing out the misconception of the often touted 2x rule which is total bunk.
    – Elder Geek
    Apr 8, 2018 at 16:09
  • @ElderGeek Thank you. I added a history section on when 16 MB RAM times 2 for 32 MB SWAP may have made sense. Apr 8, 2018 at 17:22
  • 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??? Jan 29, 2019 at 3:03
  • @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/… Jan 29, 2019 at 12:46
0

There is an overhead, indeed, but is very small; for every on-disk "page" there's a reference counter, and there's a couple of structs associated with every swap area; this is in kernel memory, so can't be paged. It's between 2 and 16 MB every 4 GB, depending on the architecture. The rest is fairly dynamic, thus dependant on usage.

So you can have plenty, but keep in mind the paged address space itself imposes some overhead on the RAM, and on top of that there's some tracking going on for swapped pages, so it doesn't scale very far; even if imposing quite some overhead, filesystems are far more efficient than the swap areas when it comes to handling larger areas as the vfs can offload structural information to disk (normally at least page tables can't be swapped, because of hardware limitations, and the vmm is likely using non-swappable memory for all it's bookkeeping).

I can't think of any other downside, OTOH having too little swap can cause fragmentation when swapping pages, which means more seeking. When having a lot, after certain point there will be no improvements either.

Swapping is generally a last resource (hibernation aside), so you'll probably not benefit from having more swap space than RAM, specially if you have over 4GB of RAM, and sometimes even as big as RAM is too much (think 64 GB).

4
  • Sorry, your last statement/sentence is confusing, otherwise a good add-on to the above. Jan 29, 2019 at 3:22
  • @NZDev is it better now? Mar 16, 2019 at 0:48
  • 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
    Jan 1, 2021 at 6:41
  • after reading answers at askubuntu.com/questions/846163/… i think it is ok.
    – qdinar
    Jan 13, 2021 at 19:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .