joe2act
Posts: 40
Joined: Tue Jul 24, 2012 4:21 am

No swap space?

Mon Aug 13, 2012 12:22 am

Hi,

Normally Linux(e.g. Ubuntu) file system has swap space. However, I found that R-Pi with Raspbian Wheezy does not have swap space.

Why Raspbian has no swap ? for increasing the lifespan of the SD card?
How can Raspbian disable swap?

Thank you in advance.

bredman
Posts: 1415
Joined: Tue Jan 17, 2012 2:38 pm

Re: No swap space?

Mon Aug 13, 2012 8:23 am

Using swap on an SD card is not recommended for 2 reasons

1. An SD card can only sustain a limited number of writes in its lifetime.
2. Read/write access to an SD card is dreadfully slow.

You can enable swap if you like, it's your choice. Search this forum for the word "swapon".

User avatar
RaTTuS
Posts: 10086
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: No swap space?

Mon Aug 13, 2012 8:25 am

1st ) free -h
2nd) raspbian uses a small swapfile
3rd) raspbina has the swapiness set to very low
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

joe2act
Posts: 40
Joined: Tue Jul 24, 2012 4:21 am

Re: No swap space?

Mon Aug 13, 2012 9:44 pm

bredman wrote:Using swap on an SD card is not recommended for 2 reasons

1. An SD card can only sustain a limited number of writes in its lifetime.
2. Read/write access to an SD card is dreadfully slow.

You can enable swap if you like, it's your choice. Search this forum for the word "swapon".
Thank you for reply.

joe2act
Posts: 40
Joined: Tue Jul 24, 2012 4:21 am

Re: No swap space?

Mon Aug 13, 2012 10:12 pm

RaTTuS wrote:1st ) free -h
2nd) raspbian uses a small swapfile
3rd) raspbina has the swapiness set to very low
Thank you for reply.

I typed your suggested command 'free -h'. I found that Raspbian Wheezy has a 99MB swap space.
However, if I use 'sudo fdisk -l' command, Raspbian lists two partitions: mmc0p1(FAT), and mmc0p2(ext4), and I found no Linux swap partition or space on SD card. Why Raspbian has two different swap spaces (one swap is 99MB in free -h, and another swap is none in fdisk -l)?

I checked manual for free and fdisk. The free command displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel. The sudo fdisk -l commands lists the partitions on Linux system.

In my understanding, fdisk -l lists a physical disk swap space which dedicates to memory swap space. The free -h command shows a swap memory space. Is my understanding correct ?

Thank you in advance.

User avatar
jojopi
Posts: 3041
Joined: Tue Oct 11, 2011 8:38 pm

Re: No swap space?

Mon Aug 13, 2012 10:43 pm

joe2act wrote:Why Raspbian has two different swap spaces (one swap is 99MB in free -h, and another swap is none in fdisk -l)?
Because, as RaTTuS said, it uses a swapfile, not a partition. "cat /proc/swaps" for details.

"fdisk -l" is not a correct method to check whether a system is configured for swap. It cannot see swapfiles, or swap that may be inside any LVM or RAID partitions. Also the type code on partitions is not enforced, so it is possible to be swapping to a partition that appears in fdisk as some other type. Most importantly, even if fdisk does list a swap partition, that does not tell you whether the system is configured to use it...

carlosfm
Posts: 132
Joined: Fri Oct 21, 2011 3:23 pm
Location: Lisbon, Portugal

Re: No swap space?

Mon Aug 13, 2012 10:46 pm

I would also like to know how to disable the swap file on raspbian, as I am using a swap partition.
A swap partition is always much faster than a file, and in this case they both reside on the same device (the SD card!).
Swap file(s) are Windoze stuff! :oops: :mrgreen:

Btw, I've never seen swap being used on my Raspbian.
vm.swappiness=1 takes care of this.
I just don't like swap files...
Do you Pi?

User avatar
jojopi
Posts: 3041
Joined: Tue Oct 11, 2011 8:38 pm

Re: No swap space?

Mon Aug 13, 2012 11:25 pm

carlosfm wrote:I would also like to know how to disable the swap file on raspbian, as I am using a swap partition.
A swap partition is always much faster than a file,
I will be amazed if you can construct a fair benchmark that can even measure a difference. Notice that swapping to a file does not update the file's timestamps, you are not allowed to truncate or write to the file while swapping to it, and you can not swap to a file that is sparse. In other words, the file is sealed off and treated exactly like a (slightly discontiguous) mini-partition.

To answer your question, "sudo update-rc.d dphys-swapfile disable" should stop the swapfile being enabled at boot.

User avatar
RaTTuS
Posts: 10086
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: No swap space?

Tue Aug 14, 2012 7:40 am

cat /etc/fstab
this will show you :-
# a swapfile is not a swap partition, so no using swapon|off from here on, use dphys-swapfile swap[on|off] for that
if you look in
/etc/init.d
you will see
cat /etc/init.d/dphys-swapfile
you can disable this from running so a swap file is never used, this means your program will just fail with out of memory error if it ever does need more than can be allocated
the raspbian swapiness is set so low it only gets used in emergencies.
you can disable the swap file and put a swap partition on an external HD if you want
swapping to a file for a partition on the SD card will not make any difference

having some swap space is useful. [YMMV]
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

carlosfm
Posts: 132
Joined: Fri Oct 21, 2011 3:23 pm
Location: Lisbon, Portugal

Re: No swap space?

Tue Aug 14, 2012 9:11 am

jojopi wrote:
carlosfm wrote:I would also like to know how to disable the swap file on raspbian, as I am using a swap partition.
A swap partition is always much faster than a file,
I will be amazed if you can construct a fair benchmark that can even measure a difference. Notice that swapping to a file does not update the file's timestamps, you are not allowed to truncate or write to the file while swapping to it, and you can not swap to a file that is sparse. In other words, the file is sealed off and treated exactly like a (slightly discontiguous) mini-partition.

To answer your question, "sudo update-rc.d dphys-swapfile disable" should stop the swapfile being enabled at boot.
I don't have proof, it's just a question of principle, but I'm not going to argue.
A swap file is contained inside a filesystem, while the swap partition is dedicated for this task.
If it was an HDD, it would make much more difference, as I can locate the swap partition at the very beginning of the disk, while a swap file resides somewhere in the filesystem, which we can not exactly control.
Oh, and thanks for your help. :mrgreen:
Do you Pi?

bredman
Posts: 1415
Joined: Tue Jan 17, 2012 2:38 pm

Re: No swap space?

Tue Aug 14, 2012 9:36 am

Some people were asking for the relevant commands...

To check the current swap status
swapon -s

To turn off all swap
sudo swapoff -a

To turn on swap as defined in /etc/fstab
sudo swapon -a

To turn on swap, using a specified file
sudo swapon /var/swap

carlosfm
Posts: 132
Joined: Fri Oct 21, 2011 3:23 pm
Location: Lisbon, Portugal

Re: No swap space?

Tue Aug 14, 2012 6:57 pm

Well...
As recommended above, I excuted:

"sudo update-rc.d dphys-swapfile disable"

This has produced errors and nothing happened.

Then I've tried what I had tried before, withot success (executed as su):

dphys-swapfile swapoff

But when booting, the damn swap file was still being activated.
I've only managed to stop it by removing the executabe flag of the "/etc/init.d/dphy-swapfile" file.
No my raspbian loads with only my swap partiton (on the SD card) and it seems faster!

PS: this post was written on my Pi.
Do you Pi?

User avatar
truehl
Posts: 642
Joined: Sun Mar 04, 2012 6:47 pm
Contact: Website

Re: No swap space?

Tue Aug 14, 2012 7:19 pm

Why not simply uninstall the dphys-swapfile package with:

Code: Select all

apt-get purge dphys-swapfile
After doing this reboot and the swapfile and the piece of SW responsible for the swap has gone!
truehl
http://www.squeezeplug.de

User avatar
jojopi
Posts: 3041
Joined: Tue Oct 11, 2011 8:38 pm

Re: No swap space?

Tue Aug 14, 2012 7:44 pm

carlosfm wrote:"sudo update-rc.d dphys-swapfile disable"
This has produced errors and nothing happened.
Are you serious? You are honestly not going to tell us what the errors said?

joe2act
Posts: 40
Joined: Tue Jul 24, 2012 4:21 am

Re: No swap space?

Tue Aug 14, 2012 7:47 pm

jojopi wrote:Because, as RaTTuS said, it uses a swapfile, not a partition. "cat /proc/swaps" for details.

"fdisk -l" is not a correct method to check whether a system is configured for swap. It cannot see swapfiles, or swap that may be inside any LVM or RAID partitions. Also the type code on partitions is not enforced, so it is possible to be swapping to a partition that appears in fdisk as some other type. Most importantly, even if fdisk does list a swap partition, that does not tell you whether the system is configured to use it...
Thank you for reply.

In Raspbian, I found that 'free' and 'cat /proc/swaps' report same swap space, 102369KB.

Code: Select all

[email protected] ~ $ cat /proc/swaps
Filename                                Type            Size    Used    Priority
/var/swap                               file            102396  12      -1

[email protected] ~ $ free
             total       used       free     shared    buffers     cached
Mem:        188080     176424      11656          0      33992     115532
-/+ buffers/cache:      26900     161180
Swap:                102396       12     102384

Linux raspberrypi 3.1.9+ #168 PREEMPT Sat Jul 14 18:56:31 BST 2012 armv6l GNU/Linux
The /etc/fstab also tell something interesting about swap space:

Code: Select all

[email protected] ~ $ cat /etc/fstab
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       0
/dev/mmcblk0p2  /               ext4    defaults,noatime  0       0
# a swapfile is not a swap partition, so no using swapon|off from here on, use  dphys-swapfile swap[on|off]  for that
Last edited by joe2act on Tue Aug 14, 2012 7:54 pm, edited 3 times in total.

carlosfm
Posts: 132
Joined: Fri Oct 21, 2011 3:23 pm
Location: Lisbon, Portugal

Re: No swap space?

Tue Aug 14, 2012 7:48 pm

jojopi wrote:
carlosfm wrote:"sudo update-rc.d dphys-swapfile disable"
This has produced errors and nothing happened.
Are you serious? You are honestly not going to tell us what the errors said?
Sorry jojopi, when I posted this I had already rebooted and didn't want to execute it again.
You can try it, it didn't work but it has made no harm, just a couple of warnings.
Do you Pi?

carlosfm
Posts: 132
Joined: Fri Oct 21, 2011 3:23 pm
Location: Lisbon, Portugal

Re: No swap space?

Tue Aug 14, 2012 7:50 pm

truehl wrote:Why not simply uninstall the dphys-swapfile package with:

Code: Select all

apt-get purge dphys-swapfile
After doing this reboot and the swapfile and the piece of SW responsible for the swap has gone!
A much more elegant solution, no doubt. :mrgreen:
Do you Pi?

User avatar
jojopi
Posts: 3041
Joined: Tue Oct 11, 2011 8:38 pm

Re: No swap space?

Tue Aug 14, 2012 8:11 pm

carlosfm wrote:You can try it, it didn't work but it has made no harm, just a couple of warnings.
But of course I did try it, prior to recommending. I can assure you it works when typed correctly on a not-otherwise-broken system. Did the errors look anything like:

Code: Select all

update-rc.d: using dependency based boot sequencing
update-rc.d: error: cannot find a LSB script for you-misspelled-dphys-swapfile
or:

Code: Select all

-bash: sudo update-rc.d dphys-swapfile disable all in quotes: command not found

carlosfm
Posts: 132
Joined: Fri Oct 21, 2011 3:23 pm
Location: Lisbon, Portugal

Re: No swap space?

Tue Aug 14, 2012 8:14 pm

No, nothing like that.
It said something was empty, and something about the runlevel.
Btw, I've copied the command from your post (without quotes) and pasted in the terminal.
Hey, my system is not broken...
Do you Pi?

User avatar
jackokring
Posts: 815
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: No swap space?

Tue Aug 14, 2012 8:24 pm

carlosfm wrote:
jojopi wrote:
carlosfm wrote:I would also like to know how to disable the swap file on raspbian, as I am using a swap partition.
A swap partition is always much faster than a file,
I will be amazed if you can construct a fair benchmark that can even measure a difference. Notice that swapping to a file does not update the file's timestamps, you are not allowed to truncate or write to the file while swapping to it, and you can not swap to a file that is sparse. In other words, the file is sealed off and treated exactly like a (slightly discontiguous) mini-partition.

To answer your question, "sudo update-rc.d dphys-swapfile disable" should stop the swapfile being enabled at boot.
I don't have proof, it's just a question of principle, but I'm not going to argue.
A swap file is contained inside a filesystem, while the swap partition is dedicated for this task.
If it was an HDD, it would make much more difference, as I can locate the swap partition at the very beginning of the disk, while a swap file resides somewhere in the filesystem, which we can not exactly control.
Oh, and thanks for your help. :mrgreen:
A swap partition is not always much faster than a swap file. It depends on the inode structure and what is in what swap page and how close on disk, or in the flash prefetch some sector is to the file data it relies on. This does of course imply the swap driver has to be optimal or close to to see the benefit of this effect.

Cheers Jacko
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

User avatar
RaTTuS
Posts: 10086
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: No swap space?

Wed Aug 15, 2012 10:57 am

and I would not expect it be be any quicker
if after a boot and
free
shows that swap is being used
then you may have other problems ;-p
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

jerrylamos
Posts: 27
Joined: Sun Jul 15, 2012 8:33 pm

Re: No swap space?

Wed Aug 15, 2012 12:35 pm

A swap partition is not always much faster than a swap file. It depends on the inode structure and what is in what swap page and how close on disk, or in the flash prefetch some sector is to the file data it relies on. This does of course imply the swap driver has to be optimal or close to to see the benefit of this effect.
So I put a swapfile on the SD card, Debian Wheezy, at 2x cache size 512 MB entered it into /etc/fstab and turned the Wheezy automatically generated swap file of about 110 MB off. Rebooted and Wheezy automatically put the swap file back again. How do I turn the swap file off other than manual command every time ?

Yes, what I do with Midori does challenge the 224 MB memory size and I haven't even found a way to play the Raspberry Pi Org's and MagPi's videos on the Pi. Attempts to run huge Gnash (lots! of packages to install) have hung up, and I haven't found a Raspberry Pi HTML5 package. Now smartphones using ARM processors play videos - why can't Pi?

User avatar
RaTTuS
Posts: 10086
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: No swap space?

Wed Aug 15, 2012 12:57 pm

apt-get purge dphys-swapfile
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

jerrylamos
Posts: 27
Joined: Sun Jul 15, 2012 8:33 pm

Re: No swap space?

Wed Aug 15, 2012 9:28 pm

RaTTuS wrote:apt-get purge dphys-swapfile
Thanks! Looked like it did something, freed up some disk space, and its still running.

joe2act
Posts: 40
Joined: Tue Jul 24, 2012 4:21 am

Re: No swap space?

Wed Aug 15, 2012 11:43 pm

Thanks to all.

In summary, Raspbian still has a swap file in file system, but Raspbian has no swap partition in SD card. The reason is SD card has limited read/write cycles. No swap partition in SD card can increase SD card life span.

Return to “General discussion”

Who is online

Users browsing this forum: ejolson and 49 guests