139

Raspberry Pi has only 256 MB of RAM, so I would like to use swap space (either on SD card or attached USB storage). How do I set it up?

5 Answers5

171

Raspbian uses dphys-swapfile, which is a swap-file based solution instead of the "standard" swap-partition based solution. It is much easier to change the size of the swap.

The configuration file is:

/etc/dphys-swapfile 

The content is very simple. By default my Raspbian has 100MB of swap:

CONF_SWAPSIZE=100

If you want to change the size, you need to modify the number and restart dphys-swapfile:

/etc/init.d/dphys-swapfile restart

Edit: On Raspbian the default location is /var/swap, which is (of course) located on the SD card. I think it is a bad idea, so I would like to point out, that the /etc/dphys-swapfile can have the following option too: CONF_SWAPFILE=/media/btsync/swapfile

I only problem with it, the usb storage is automounted, so a potential race here (automount vs. swapon)

asalamon74
  • 4,108
  • 4
  • 31
  • 31
39

You can set up swap space quite simply. For example, if your USB drive is /dev/sdx, you would use (you must be root for this):

$ mkswap /dev/sdx
$ swapon /dev/sdx

Note that this would use the whole device and you will probably lose all the existing data on it.

You can also create a swap file (by using a loop device) like this:

$ dd if=/dev/zero of=/path/to/swapfile bs=1M count=1024 # For 1GB swap file
$ mkswap /path/to/swapfile
$ swapon /path/to/swapfile

When you no longer need the swap file (if you want to eject the USB drive for example), you must use swapoff <device>. Not doing so will probably result in system crash.

You should be careful though. SD cards have limited read/write limits and it will shorten its lifespan. If you are using an external hard drive, you should be fine, but it will be very slow.

Andrew Larsson
  • 1,614
  • 17
  • 22
36

Do not do this at all.

You should not enable swap on the Raspberry Pi.

Although it is possible, it is not useful. Even on a class 10 SDHC card, it is just too slow. Also you will reduce the lifespan of the SD card.

On any flash-based storage device (SD card, SDD, USB thumb drives) you are also likely to see system-wide pauses while a large group of flash blocks is erased.

Possible exceptions:

  • If you connect a (magnetic) hard drive (though a USB-SATA or USB-IDE adapter)
  • If you use ZRAM or something similar
finnw
  • 5,790
  • 3
  • 34
  • 42
4

Raspbmc uses /etc/init/swap.conf to configure swap via /swap file. It first checks for presence of /home/pi/.enable_swap.

If you delete /home/pi/.enable_swap then swap file is not created, and then just recreate it with touch /home/pi/.enable_swap if you need swap turned on and reboot.

Ghanima
  • 15,958
  • 17
  • 65
  • 125
valentt
  • 1,315
  • 3
  • 15
  • 21
3

It is a pity that the Raspberry Pis do not have GigaBit Ethernet but it is at least theoretically possible to have swap space on a network device - the Linux Terminal Server Project can offer it from the server to the clients according to this item on their wiki.

I found a Foundation Forum topic "Tip: Swap over nfs" that shows how someone who already had some NFS mounts already in place used a swapfile on one of them and mounted it via a loop-mount (possibly needed because Linux does not allow a NFS mount to be used directly). Given that the remote swap-file will hold data that the OS must not lose I'd only consider this for a wired Ethernet network - a wireless link would be too fragile IMHO. Also, of course you must not allow the remote server to be shut-down without turning the swap off and allowing it to completely empty first!

SlySven
  • 3,631
  • 1
  • 20
  • 46