8

As far as I know, the theoretical limit for a swap file is insanely large depending on whether a 32bit or 64bit system is used.

I have made a 16GB swap file on a 4GB RAM 64bit system with otherwise low-average specs, because I have more than enough space on my 1TB internal HDD...

But does the large swap file actually make a difference in speed/performance?

  • 1
    Related: askubuntu.com/q/49109/367990 – Byte Commander May 27 '18 at 22:52
  • 2
    IMO if you are using swap you really need more RAM, not a larger swap file – Panther May 27 '18 at 23:35
  • 1
    if you're using HDDs then you should use zram instead and lower the priority of the HDD swaps. And such a big swap partition won't help, because by the type you use like 4GB swap your system had been slow to a crawl – phuclv May 28 '18 at 6:00
  • 2
    Related: askubuntu.com/q/1017021/504066 – PerlDuck May 28 '18 at 9:49
  • As far as I recall a swap file will consume some amount of physical RAM (I think less than 1% of the size of the swap file). So having an excessively large swap file could hurt performance due to that memory consumption. But for a reasonably sized swap file, that's not going to be a problem. – kasperd May 28 '18 at 12:09
6

It makes no difference in terms of speed or performance. It just allows you to use up to 20 GB of virtual memory before the OOM killer starts killing processes to reclaim memory.

If you want better performance:

  • decrease swappiness if you don't use more than 4 GB that often
  • add RAM
  • switch to an SSD and put the swap file on it
  • If you often come into the point of having to swap out memory blocks, a SSD due their architecture and limited write cycles is not the way to go because it can easily use up the SSD's write cycles very quickly. If you only occasionally come into the need of swap memory SSD's are fine. – Videonauth May 27 '18 at 23:13
  • 11
    @Videonauth: "Easily use up the SSD's write cycles very quickly": any kind of half-decent modern SSD can sustain writing the full capacity of the SSD day after day for years. Your advice might have been correct in 2010, but it's out of date. Even a consumer-grade 256 GiB Samsung SSD 840 can sustain writing 100 GiB per day for more than 2 years. – AlexP May 28 '18 at 0:30
  • If you have lots of RAM, decreasing swappiness makes even less difference than if you had limited RAM. Actually installing lots of RAM makes any other tweaks to swap redundant if it's enough that you can avoid swapping. Keep a swap partition for stability "just in case", 99.9% of the time it won't be used, and you'll retain great performance. – thomasrutter May 29 '18 at 0:12
  • 1
    Also, the thing about limited SSD write cycles has been untrue for over a decade, it's really time this FUD needs to be called out. Sorry this comment is so redundant given that AlexP already addressed this, but it bears underlining. SSDs are great for write-heavy applications. – thomasrutter May 29 '18 at 0:14
  • 1
    Wear leveling in SSDs became a thing around 2008 (2008 paper | 2009 Anandtech article). "Did laptops at normal consumer price points even come with SSDs in 2008" heck no, SSDs were crazy expensive and didn't really come standard. – thomasrutter May 29 '18 at 15:21
6

You can have up to 32 swap areas (files or partitions) with up to ~4 billion (2^32) pages each, where a "page" is 4kB on a traditional (eg x86 or x86_64) system but can be larger on other architectures or if you have configured large pages specially.

This gives a theoretical maximum of 16TB per swap area, and up to 32 swap areas. There may be other limitations that apply before you reach this but this is as big a swap area that mkswap can create for you.

As to how much you want, any swap area beyond the maximum you actually utilize contributes nothing to system performance or stability. The trick is to estimate the maximum amount of utilization that will ever occur in a normal or even abnormal situation, and use that as your swap file size and no more.

At time of writing, swap space greater than around 4GB more than your physical memory size is very unlikely to be utilized, unless your system is very abnormal. On a typical system such as a desktop system used by one person at a time, you can probably even stop at a maximum of say 6GB to 8GB even if your physical memory is greater.

3

According to the answer by Gilles

...There used to be a 2GB swap size limit (on most 32-bit platforms), but this has not been the case for years. The limitation was removed at some point in the 2.2 kernel series.

Additionally, he cites man page for mkswap:

The maximum useful size of a swap area depends on the architecture and the kernel version.

In linux/swap.h library there is MAX_SWAPFILES entry, so there is limit on the number of swap files on the kernel level, however that apparently isn't definitive area either. According to the linked document:

It should be noted that the six bits for “type” should allow up to 64 swap areas to exist in a 32 bit architecture instead of the MAX_SWAPFILES restriction of 32

Which I understand has having maximum of up to 64 swap files on the 32-bit architectures. However, I haven't noticed in the document mention what would happen on 64-bit architecture. Also worth noting that in this case, OP's efforts to create swap files failed on 30th entry.

As for swap partitions, there appears to be no limit for that in the kernel, besides previously mentioned constats, but number of any partitions still would be limited by the underlying BIOS and disk layout.

2

This answer might not be appropriate for Ubuntu 18.04 (thanks to @Chai T. Rex for pointing this out in the comments).

Anyway for previous versions, here it goes (there is still no proposed update LTS>LTS, so this might still be relevant):

It's not a file, it's a partition, and yes it does because of swappiness

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

So basically if your swap partition is much bigger than the RAM that you have, the system will tend to swap more, causing a general slowness.

  • 4
    There are swap files now, and they're the default (instead of partitions) when installing 18.04 from scratch. – Chai T. Rex May 27 '18 at 22:47
  • 1
    I assume the behaviour of swap pretty much stays the same, no matter whether it's backed by a partition or file. You can also have multiple swap files and/or partitions at the same time. Anyway, I don't think more swap would have a negative effect while there's still enough RAM left. – Byte Commander May 27 '18 at 22:55
  • I would assume the same, but until I'll get my hands on it, this is just a wild assumption – dadexix86 May 27 '18 at 22:56
  • There swap partitions and there are swap files. Swap partitions can be used by two or more Linux installations on the same PC (dual boot or triple boot). Swap partition are better in case of full disk where the swap file could be created fragmented. ... The swap files will work as good as the swap partitions if they are created on non-full disks to avoid fragmentation – Devon van Schoor j – Devon van Schoor May 27 '18 at 23:00
2

There is not much difference in speed or performance when increasing the swap partition to be arbitrarily large.

There are several factors related to swap that can affect performance such as swappiness and hard drive type (SDD or HDD).

Swap is fairly slow and increasing swappiness reduces the performance. However it only has an affect if there is not enough RAM. But it is better than not having any swap. My recommendation is to make the swap partition size twice as big as your RAM.

  • 5
    that recommendation is outdated. Modern systems with 32 or 64GB RAM won't need 64 or 128GB swap – phuclv May 28 '18 at 6:07
  • 3
    @LưuVĩnhPhúc I wouldn't use more than 2GB of swap these days... – el.pescado May 28 '18 at 7:19
  • 2gb seems very low ... virtual machines or games could really fill more than 2gb of swap very quickly. I think 4 to 8gb of swap works well with any amount of ram. If you need more than 8gb then you should probably add more ram. – Germain May 28 '18 at 13:46
0

I arrive at the fact that a swap file or swap partition practically have no limit. Also, my 16GB swap file is quite big but the size does not affect the speed.

However what I gather is that what does effect the speed is the system actually making use of that swap space as opposed to the physical hardware.

My solution to that is to make a high swappiness value so that the swap is not overly used as opposed to the faster hardware. so I did:

sudo -s
nano /etc/sysctl.conf

Added to file:

#vm.swappiness = 50

save

sudo sysctl -p

to refresh (or could have just rebooted)

  • Adding the line #vm.swappiness = 50 to /etc/sysctl.conf doesn't have any effect, the leading # makes this line to be a comment, remove it. You can check swappiness with cat /proc/sys/vm/swappiness. – mook765 May 28 '18 at 6:27
  • But the method I described did work. When I input [cat /proc/sys/vm/swappiness] I get 50 as the output – Devon van Schoor May 28 '18 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.