18

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?

2
  • There's no Default value it depends on how much RAM you have. Jun 24, 2011 at 8:18
  • 3
    Well 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, 2011 at 8:35

2 Answers 2

20

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.

1
  • Pls consider to update the 'Source code' link, it does not exist anymore. Apr 4 at 21:07
7

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:

7
  • Interesting debate: a better reference than a strange debian website?
    – enzotib
    Jun 24, 2011 at 18:31
  • 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, 2011 at 18:49
  • @enzotib: why strange?
    – arrange
    Jun 24, 2011 at 18:56
  • @arrange: strange because not an official ubuntu site, and also as a debian site do not seems an official one.
    – enzotib
    Jun 24, 2011 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, 2011 at 21:35

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.