gabello
Posts: 51
Joined: Sun Aug 05, 2012 8:02 pm

Swap file/partition

Tue Aug 07, 2012 12:00 am

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

SirLagz
Posts: 1705
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
Contact: Website

Re: Swap file/partition

Tue Aug 07, 2012 1:50 am

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.
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044

User avatar
accuser
Posts: 8
Joined: Fri Jul 20, 2012 4:33 am
Location: Cornwall, UK
Contact: Website

Re: Swap file/partition

Tue Aug 07, 2012 5:39 am

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

Heater
Posts: 13600
Joined: Tue Jul 17, 2012 3:02 pm

Re: Swap file/partition

Tue Aug 07, 2012 10:29 am

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.
Memory in C++ is a leaky abstraction .

gabello
Posts: 51
Joined: Sun Aug 05, 2012 8:02 pm

Re: Swap file/partition

Tue Aug 07, 2012 11:53 am

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.

Heater
Posts: 13600
Joined: Tue Jul 17, 2012 3:02 pm

Re: Swap file/partition

Tue Aug 07, 2012 12:41 pm

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.
Memory in C++ is a leaky abstraction .

gabello
Posts: 51
Joined: Sun Aug 05, 2012 8:02 pm

Re: Swap file/partition

Tue Aug 07, 2012 12:47 pm

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) ;)

mcgyver83
Posts: 358
Joined: Fri Oct 05, 2012 11:49 am

Re: Swap file/partition

Wed Feb 12, 2014 9:09 am

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?

User avatar
rpdom
Posts: 15362
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Swap file/partition

Wed Feb 12, 2014 9:25 am

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.

mcgyver83
Posts: 358
Joined: Fri Oct 05, 2012 11:49 am

Re: Swap file/partition

Wed Feb 12, 2014 9:45 am

Ok, but I want only to disable it, test the new solution and after remove the package if not need.

User avatar
rpdom
Posts: 15362
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Swap file/partition

Wed Feb 12, 2014 10:05 am

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.

User avatar
ab1jx
Posts: 868
Joined: Thu Sep 26, 2013 1:54 pm
Location: Heath, MA USA
Contact: Website

Re: Swap file/partition

Wed Jan 31, 2018 1:14 am

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.

Return to “Raspbian”