# How do I disable swap?



## PeteS (Jan 3, 2011)

After installing, compiling and reconfiguring I like to disable swap completely.

How do I go about it? Would it be enough to remove the swap partition from /etc/fstab?

Thanks,
Pete



`# swapctl -l`

```
Device:       1024-blocks     Used:
/dev/ad4s1b     2029408         0
```


This is what /etc/fstab looks like:

```
# Device		Mountpoint	FStype	Options		Dump	Pass#
/dev/ad4s1b		none		swap	sw		0	0
/dev/ad4s1a		/		ufs	rw		1	1
#/dev/ad4s1e		/tmp		ufs	rw		2	2
/dev/ad4s1f		/usr		ufs	rw		2	2
/dev/ad4s1d		/var		ufs	rw		2	2
```


----------



## graudeejs (Jan 3, 2011)

You'd need to repartition your HDD, if you want to use space that was used from swap for something else.

Of you could edit BSD labels (remove b label) and grow root fs with growfs.
Whatever you do make backups.

Or you can simply use swap, and repartition disk, next time you do clean install (or something like that)


----------



## wblock@ (Jan 3, 2011)

PeteS said:
			
		

> After installing, compiling and reconfiguring I like to disable swap completely.



Why?  It provides a nice safety net.



> How do I go about it? Would it be enough to remove the swap partition from /etc/fstab?



Yes, according to swapoff(8).


----------



## PeteS (Jan 3, 2011)

So if I just remove the partition, one way or the other, swap will be gone and there will no temporary swapfile or anything like that created?


----------



## wblock@ (Jan 3, 2011)

Remove the entry from /etc/fstab before removing the partition.  You can set a swapfile in /etc/rc.conf, but it's disabled by default.

So yes, that should completely disable swap (untested--I want my swap!).  Still curious why you want to do that, though.


----------



## PeteS (Jan 3, 2011)

I want to remove swap to keep the system idle as much as possible, use a minimum amount of power and never use the disk unless needed and it looks like I have plenty of RAM available so swap should never be needed. This is not a desktop or general purpose system.

I have removed swap now and this is output from `# top`

```
last pid:  1426;  load averages:  0.00,  0.00,  0.00
29 processes:  1 running, 28 sleeping
CPU:  0.0% user,  0.0% nice,  0.1% system,  0.1% interrupt, 99.8% idle
Mem: 9596K Active, 6644K Inact, 35M Wired, 8K Cache, 10M Buf, 926M Free
Swap:
```


----------



## wblock@ (Jan 3, 2011)

Swap isn't used unless it's needed, anyway.  Actually, that should be qualified: on FreeBSD, swap isn't used unless it's needed.  Other operating systems--which will not be named except to say that it's Windows--swap before they're out of RAM.  I've asked why, but never really had a satisfactory answer.  Best I can come up with is that UnnamedOS moves things into swap so that RAM will be free and available.  So instead of being slow when out of RAM, it's slow all the time.

Anyway, FreeBSD only uses swap when RAM is full.


----------



## PeteS (Jan 3, 2011)

wblock said:
			
		

> Anyway, FreeBSD only uses swap when RAM is full.



Thanks for the clarification. That's good to know. And thanks for all the help so far. I appreciate it.


----------



## SirDice (Jan 4, 2011)

PeteS said:
			
		

> wblock said:
> 
> 
> 
> ...


Actually, it does use swap, even if there's enough RAM. Modern operating systems will always use some swap.

Swap usage in and of itself isn't bad. It's the excessive paging that will make things slow.

If you have enough memory you might consider creating a RAM drive and putting the swap on there. This might sound counter intuitive but the OS will benefit from having at least some swap.


----------



## aragon (Jan 4, 2011)

If you're feeling extra pedantic, you could recompile your kernel with:


```
options NO_SWAPPING
```


----------



## wblock@ (Jan 4, 2011)

SirDice said:
			
		

> Actually, it does use swap, even if there's enough RAM. Modern operating systems will always use some swap.



ITYM "may use swap even when memory isn't full" but am not sure.  This machine says "Swap: 4096M Total, 4096M Free", but it's only been up two days.



> If you have enough memory you might consider creating a RAM drive and putting the swap on there. This might sound counter intuitive but the OS will benefit from having at least some swap.



http://xkcd.com/285/


----------



## SirDice (Jan 4, 2011)

From tuning(7):


> It is not recommended that you configure any less than 256M of swap on a system and you should keep in mind future memory expansion when sizing the swap partition. The kernelâ€™s VM paging algorithms are tuned to perform best when there is at least 2x swap versus main memory. Configuring too little swap can lead to inefficiencies in the VM page scanning code as well as create issues later on if you add more memory to your machine.



You may also like to read Design elements of the FreeBSD VM system.


----------



## jalla (Jan 4, 2011)

Another point; should you ever want to keep a crashdump for analysis, your swap must be atleast as big as physical memory.


----------



## free-and-bsd (Aug 13, 2013)

Hi, it's been a long time since the last post on this thread.

Within these 2-3 years some changes have taken place both in FreeBSD design (I presume) and hardware specifications of even average user systems. For example, now I'm configuring a (FreeNAS based) system with 16 GB of RAM. ZFS, too, needs a good deal of RAM, yet for all I've read 16 GB must be more than enough for that.

So, the problem I'm facing now is the same as the OP starter had: I don't want unneeded activity on data storage drives. So how do we stand now, two years after the last post in this thread, regarding  swap space? What are the reasons to consider it necessary for the system and _is_ it actually _so_ on a system with 16 GB of RAM?

Sorry if my question somehow duplicates ones already asked and answered, yet the changes mentioned above may have changed things. FreeNAS documentation, understandably, doesn't go into such detail, but I want to be sure and do the right thing to build a stable and effective system.


----------



## kpa (Aug 13, 2013)

Sure you can use the system without swap, no problem. Swap won't be used until it is really necessary to put some not so recently used pages to rest to make room for pages of active processes. 

However, swap is a safety measure that can save your system from memory exhaustion when there are many large services running on it and the size of the resident set of those processes hovers near the amount of your physical memory. 

Do note that memory taken by ZFS caching is never swapped, the same goes for many kernel data structures that have to be in wired memory so swap is mainly used for user space processes.


----------



## SirDice (Aug 13, 2013)

Swap usage in and of itself isn't a problem. It's excessive swapping that causes problems.


----------



## free-and-bsd (Aug 13, 2013)

OK thanks, good to know all this. 

Meantime I've tried to comment out the swap lines in /etc/fstab and `#swapoff`, but on the next reboot swap was back there. Perhaps, because I didn't remove the swap partitions from ada0 and ada1 -- the system allocated 2 GB for swap on each physical disk. 

OK! Whatever the system does it does for a reason, and also they bring out the point that this allocates less space for ZFS volumes, which makes it more secure in case you should need to replace a failed disk in the pool: with `zpool` you can replace either with equal or with larger volume. So a ZFS volume of 915 GB means, then, that I can replace a 1 TB drive with another 1 TB drive yet, increasing the volume slightly.

I'll look into it deeper, meantime let the swap partitions remain as they are. This FreeNAS system, though it _is_ FreeBSD, has its own schedule and configuration which it is not wise to alter without a good understanding of what it does and why.


----------



## SirDice (Aug 13, 2013)

free-and-bsd said:
			
		

> Meantime I've tried to comment out the swap lines in /etc/fstab and `#swapoff`, but on the next reboot swap was back there. Perhaps, because I didn't remove the swap partitions from ada0 and ada1 -- the system allocated 2G for swap on each physical disk.


Removing the entries from fstab should be all that's needed. There's no other place the system looks when enabling swap.


----------



## kpa (Aug 13, 2013)

Swap on ZVOL is started by /etc/rc.d/zvol and it looks at the org.freebsd:swap property. If the value is "on" the volume is used as swap.


----------



## free-and-bsd (Aug 15, 2013)

This swap isn't via ZFS swap. It's via GPT swap partition `type=freebsd-swap` on each of two data disks like this one:
	
	



```
#gpart show ada0                                               
=>        34  1953525101  ada0  GPT  (931G)                                     
          34          94        - free -  (47k)                                 
         [B]128     4194304     1  freebsd-swap  (2.0G)[/B]                            
     4194432  1949330696     2  freebsd-zfs  (929G)                             
  1953525128           7        - free -  (3.5k)
```
And what is interesting, `#swapctl -l` shows the following:
	
	



```
#swapctl -l                                                    
Device:       512-blocks      Used:                                             
/dev/ada0p1.eli    4194304          0                                           
/dev/ada1p1.eli    4194304          0
```

It seems, although you were right regarding swap handling in FreeBSD, in FreeNAS it's done differently. As I mentioned, removing it from /etc/fstab didn't do the thing. And the manual says it is set through the web GUI, and in GUI it comments next to the field where SWAP size is to be set:





> Swap size on each drive in *GiB*, affects new disks only. Setting this to 0 disables swap creation completely (STRONGLY DISCOURAGED).


 Interesting, too, that in FreeNAS files like /etc/rc.conf and /etc/fstab are kind of secondary and configuration is saved elsewhere.


----------



## SirDice (Aug 15, 2013)

free-and-bsd said:
			
		

> Interesting, too, that in FreeNAS files like /etc/rc.conf and /etc/fstab are kind of secondary and configuration is saved elsewhere.


Which is why we have this thread: [thread=7290]PC-BSD DesktopBSD FreeNAS NAS4Free m0N0WALL pfSense ArchBSD kFreeBSD JabirOS topics[/thread]


----------



## free-and-bsd (Aug 15, 2013)

Right !


----------



## throAU (Aug 29, 2013)

wblock@ said:
			
		

> Swap isn't used unless it's needed, anyway.  Actually, that should be qualified: on FreeBSD, swap isn't used unless it's needed.  Other operating systems--which will not be named except to say that it's Windows--swap before they're out of RAM.  I've asked why, but never really had a satisfactory answer.  Best I can come up with is that UnnamedOS moves things into swap so that RAM will be free and available.  So instead of being slow when out of RAM, it's slow all the time.
> 
> Anyway, FreeBSD only uses swap when RAM is full.



I can actually see the logic behind swapping idle processes to disk pro-actively.

Say you have a 1 gigabyte system with 500 megabytes available.  Of your 500 megabytes (roughly) of consumed memory, 120 megabytes is "idle".

If you pro-actively swap that to disk, an alloc for say, 550 megabytes of RAM will be instant as you won't need to start paging to disk before the alloc can occur - you simply mark the idle process memory as free as it is on disk already anyway.  If the process wakes up, you just mark the pages it modifies as dirty and page them when it goes idle again.

Also, by pro-actively paging idle processes to disk, you could potentially use the RAM they were consuming for disk cache for another process which is hitting the disk hard.

I'm pretty sure this is how OS X on Lion and later seems to page to disk, from my observations.  They've obviously played with the thresholds a bit, as Mavericks is way snappier than Lion was on release (I suspect Lion was tested internally only on SSD), but the theory seems to hold true when compared to what I see happening on my OS X box.

Waiting until you run out of RAM before paging means that typically, your performance goes to poo (due to THEN needing to start paging out) right when your machine is working hard (which would tend to be when you want it to be doing work, rather than paging excessively).  Paging out idle processes pro-actively, well in advance could help reduce that.

In other words what I'm saying is this:  aggressively swapping out could potentially improve performance due to more RAM being available for disk cache, for processes which are doing heavy IO.


----------



## wblock@ (Aug 29, 2013)

Back in the old days, on both minicomputers where Unix was created and Windows systems, there was never enough RAM.  So it's tempting to optimize for that situation by swapping out early.  But if you have a current system with plenty of RAM, the performance hits when an idle process is swapped out and then back in are unnecessary and wasteful if there was never really a reason to swap that idle process out anyway.

The system should base how it deals with swap on the current situation rather than assumptions.  FreeBSD gets this right, in my experience.


----------



## throAU (Aug 30, 2013)

This is the thing - just because you have swapped a process to disk, it doesn't mean you need to also remove it from memory pro-actively.

If it is sleeping, and memory is plentiful (and the disk is relatively idle):
- Page it to disk. leave resident.  if it wakes up briefly and modifies a page, mark it dirty and page it again.
- If the RAM is required elsewhere, write out the small percentage of remaining/newly dirty pages and remove from RAM - the vast majority if not all of it is paged out already.  If you had say, 1 megabyte of active data in 250 megabytes worth of application data, you've just saved 249 megabytes worth of paging out whilst trying to fulfil a memory allocation due to the system suddenly coming under additional load.

Essentially you are writing it out to disk and keeping it resident in memory AS WELL, so that when you DO need to page it out, it's already done - rather than being hit with a big malloc that you THEN need to try and page stuff out to fill.

OS X in Mavericks also adds memory compression, which I suspect is used as a halfway house between the initial page out (and keep resident) and dropping it from RAM entirely.

Obviously the thresholds for when to consider something idle and active (for paging) are numbers that would need to be tweaked (and this is why I suspect Lion was such a dog on the Mac - the numbers were too SSD-biased), but I believe the theory to be sound.


----------

