Tell me more ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

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?

share|improve this question
6  
Swap on the Pi (and similar devices) can be painfully slow, and anything that actually ends up using swap extensively will practically bring the system to a standstill. Careful! – MattJ Jun 12 '12 at 21:40
@MattJ Agreed. Definitely not a great idea. – Jivings Jun 12 '12 at 23:04
1  
You know what would be good is a USB RAM drive (not flash or SSD but actual volatile ram chip running at USB speeds) It would do good for a such a swap pretty good. – ppumkin Aug 15 '12 at 9:34

4 Answers

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
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.

share|improve this answer
Why add the loopback? Surely that's not necessary? – popey Jun 12 '12 at 21:47
You're right, it is not. I edited the answer to reflect that. – Tibor Jun 12 '12 at 21:49
1  
I think you should change this to avoid users copy and pasting and accidentally running mkswap on their root partition. I think sdx is a good convention. – Jivings Jun 12 '12 at 23:21
2  
Users who choose to enable SWAP may be interested in adjusting kernel swappiness. – earthmeLon Jul 15 '12 at 8:25

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
share|improve this answer
1  
no doubt swapping on a USB bey will kill your key very fast, swapping on the SD card is also dangerous, even if newer ( class 10 ) SD cards could support it better. ZRAM is clearly the way to go if you need more RAM – neofutur Jun 22 '12 at 16:05
You would think that with the ReadyBoost technology in Windows that someone would make a USB Drive (or eSata, not sure if ReadyBoost can use that, would be of no use to raspberry pi, but would be interesting) that used actual RAM so you could boost your computer performance. Although it would probably be easier and cheaper to just buy a new motherboard that supported the amount of RAM you need. – Kibbee Aug 15 '12 at 12:39

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 stop
/etc/init.d/dphys-swapfile start
share|improve this answer

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.

share|improve this answer

protected by Jivings Jun 19 '12 at 13:47

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

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