How does the installer calculate what size of swap partition to create? Presumably it's based on installed RAM size, but does that depend on how much RAM?
-
There's no Default value it depends on how much RAM you have. – Uri Herrera Jun 24 '11 at 08:18
-
3Well yes, but I was looking for a little more detail than "it depends". Is it always a set multiple of RAM size, or does it depend on how much RAM there is? For example, large RAM sizes would never need more than enough to enable hibernation, but very small RAM sizes could benefit from a swap considerably bigger than 1:1. – Seret Jun 24 '11 at 08:35
2 Answers
This is going to be rather technical but was fun to find out so here goes...
- I found a package
dphys-swapfile
and here is the Source code. Optionsetup
is used to set it up:
setup - tells dphys-swapfile to compute the optimal swap file size and (re-)generate an fitting swap file. Default it 2 times RAM size. This can be called at boot time, so the file allways stays the right size for current RAM, or run by hand whenever RAM size has changed.
- Inside
dphys-swapfile
is a settingconf_swapsize
for a pre-defined size (in Natty this is empty):
size we want to force it to be, default (empty) gives 2*RAM CONF_SWAPSIZE=
and a setting for a swapfactor
...
this is what we want, 2 times RAM size
SWAPFACTOR=2
The actual size is calculated a bit further on:
if [ "${CONF_SWAPSIZE}" = "" ] ; then # compute automatic optimal size echo -n "computing size, " # this seems to be the nearest to physical RAM size, lacks about 60k KCORESIZE="`ls -l /proc/kcore | awk '{ print $5 }'`" # make MBytes which rounded down will be exactly 1 too few, so add 1 MEMSIZE="`expr "${KCORESIZE}" / 1048576 + 1`" # default, without config file overwriding, swap=2*RAM CONF_SWAPSIZE="`expr "${MEMSIZE}" '*' "${SWAPFACTOR}"`" fi
As you can see the way they calculate it in this package depends on the size of /proc/kcore
, then gets divided by 1048576, +1 to round it up and then gets multiplied by swapfactor. From my machine:
So the default for this system would be 1065349120 / 1048576 = 1015+1 = 1016 * 2 = 2032 MBytes.

- 299,756
-
Pls consider to update the 'Source code' link, it does not exist anymore. – Manuel Jordan Apr 04 '22 at 21:07
Actually there is no dphys-swapfile
program on a default Ubuntu installation CD and it is not used to calculate the swap size.
What happens is that the ubiquity
installer uses the partman-auto
scripts and configuration files (called recipes) to determine the sizes of all partitions. It works like this:
- Partman finds the right recipe file according to the type of the computer and the option the user has chosen.
- There it finds the minimum and maximum size of the partition and its priority. For swap it can be 96 (min - in MB) 512 (priority) 300% (max).
- Then it gets the RAM size (via
/proc/meminfo
). - It uses its own algorithm to calculate the sizes.
Details:
A recipe file can look like this:
1 1 1 free
$iflabel{ gpt }
method{ biosgrub } .
500 10000 -1 $default_filesystem
$lvmok{ }
method{ format }
format{ }
mountpoint{ / } .
96 512 300% linux-swap
$lvmok{ }
method{ swap }
format{ } .
The algorithm to calculate the sizes:
for(i=1;i<=N;i++) {
factor[i] = priority[i] - min[i];
}
ready = FALSE;
while (! ready) {
minsum = min[1] + min[2] + ... + min[N];
factsum = factor[1] + factor[2] + ... + factor[N];
ready = TRUE;
for(i=1;i<=N;i++) {
x = min[i] + (free_space - minsum) * factor[i] / factsum;
if (x > max[i])
x = max[i];
if (x != min[i]) {
ready = FALSE;
min[i] = x;
}
}
}
For more see:

- 26,704

- 14,959
-
-
Well I found that too when I searched for it but the package has a "Warning: This package is intended for the use in building debian-installer images only. Do not install it on a normal Ubuntu system.". I assumed this was not used so I searched on and found the code for dphys-swapfile >:-D Hmm interesting indeed. – Rinzwind Jun 24 '11 at 18:49
-
-
@arrange: strange because not an official ubuntu site, and also as a debian site do not seems an official one. – enzotib Jun 24 '11 at 20:09
-
I found 2 more methods on calculating swap size D: This method uses grep ^Mem: /proc/meminfo or if that is 0 grep ^MemTotal: /proc/meminfo and then multiplies by 2 (default) or 3 (most are set to 300%) with a minimum per architecture. A mipsel-sb1-bcm91250a uses a fixed 512. i386-efi uses 200%. – Rinzwind Jun 24 '11 at 21:35
-
@enzotib: I can't find a better reference than the source code and the original doc files, be it on a "strange website", sorry :) – arrange Jun 24 '11 at 22:15
-
@arrange: i give a look at the installed packages and at the installer in a virtual machine: no
dphys-swapfile
, but found references to swap and recipes in variouspartman
files, so i tend to give you credit now, even if cannot understand the whole mechanism behind. You get an upvote. – enzotib Jun 25 '11 at 10:32