What is the default swap size?

Solution 1:

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. Option setup 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 setting conf_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:

enter image description here

So the default for this system would be 1065349120 / 1048576 = 1015+1 = 1016 * 2 = 2032 MBytes.

Solution 2:

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:

  • partman-auto-recipe.txtWay Back Machine

  • http://dev.blankonlinux.or.id/browser/pattimura/ubiquity/d-i/source/partman-auto/lib/recipes.sh?rev=pattimura%2Cubiquity%2C261 DEAD LINK