Page 1 of 1

Swap file/partition

Posted: Tue Aug 07, 2012 12:00 am
by gabello
Hello, would it help (performance wise) to create a swap file/partition for a Raspbian installation. If yes what is the best procedure?
Currently I have:

(parted) print all
Model: SD SD (sd/mmc)
Disk /dev/mmcblk0: 7929MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 4194kB 62.9MB 58.7MB primary fat16 lba
2 62.9MB 7929MB 7866MB primary ext4

thanks

Re: Swap file/partition

Posted: Tue Aug 07, 2012 1:50 am
by SirLagz
It can help when you run memory intensive tasks, i.e. compiling stuff.

If you do run a swap partition, it is best to have it on a USB stick / hard drive as SD Card access is not very fast at the moment.

Re: Swap file/partition

Posted: Tue Aug 07, 2012 5:39 am
by accuser
Personally, I would only put a swap partition on a physical hard disk, and not attempt to use one with an SD card or a USB drive, unless you were prepared to lose it. For more information: http://www.raspberrypi.org/phpBB3/viewtopic.php?p=39417

Re: Swap file/partition

Posted: Tue Aug 07, 2012 10:29 am
by Heater
gabello,

A swap partition is not designed to enhance performance and if your program/data is constantly being swapped from RAM to swap space performance will slow by a factor of hundreds or thousands. This would be called "thrashing".

What the swap technique does is help run a lot of programs in limited RAM space reducing the possibility of an "out of memory" failure. This might be useful in a multi-user system or other server system. Imagine a hundred people logged in and editing some files, they are all working quite slowly and the overhead of swapping from one users data to another as needed may be quite acceptable.

In that way swap helps performance by not having things crash out when they run out of memory!

As has been said before I would not put a swap partition on an SD or USB stick because it is slow and for fear of wearing it out.

Re: Swap file/partition

Posted: Tue Aug 07, 2012 11:53 am
by gabello
Thanks all for replies,

I understand the usage of the swap file, I will wait and test/try it once I get an active usb hub to connect my external HDD.

Re: Swap file/partition

Posted: Tue Aug 07, 2012 12:41 pm
by Heater
Of course having a swap file or partition on a removable media like a USB hard drive might be dangerous. Yank the USB cable out while all you heavily swapped apps are running and the all die.

Re: Swap file/partition

Posted: Tue Aug 07, 2012 12:47 pm
by gabello
I understand, this is why I said I want just to test it for now, in the end the RPi is for learning (hard or easy way) ;)

Re: Swap file/partition

Posted: Wed Feb 12, 2014 9:09 am
by mcgyver83
I'm looking for moving the raspberry from swap file on sd to swap partition on an external USB HDD.
I created the swap partition and added it to fstab, now what are the right steps to disable swap file and enable swap partition?

Re: Swap file/partition

Posted: Wed Feb 12, 2014 9:25 am
by rpdom
Remove the package dphys-swapfile. That will disable the swapfile method. You may need to delete the file manually after rebooting for the first time, I dunno, never tried it.

Re: Swap file/partition

Posted: Wed Feb 12, 2014 9:45 am
by mcgyver83
Ok, but I want only to disable it, test the new solution and after remove the package if not need.

Re: Swap file/partition

Posted: Wed Feb 12, 2014 10:05 am
by rpdom
mcgyver83 wrote:Ok, but I want only to disable it, test the new solution and after remove the package if not need.
Ok, "sudo dphys-swapfile swapoff" will turn it off until the next boot, I believe.

Re: Swap file/partition

Posted: Wed Jan 31, 2018 1:14 am
by ab1jx
Why not just edit /etc/dphys-swapfile and set it to the size you want? Not much point on a Pi with a fixed 1 GB of RAM letting it stay dynamic.

I mostly have a hard drive with a swap partition but I got tired of crashing because I ran out of swap with just an SD. Had to look up how to change it.