# swap on zpools



## Bront_in_Hood (Aug 2, 2012)

Recently I've installed FreeBSD 9.1 using this article: http://www.aisecure.net/2012/01/16/rootzfs/
This is not first time I installing FreeBSD on root zfs using such instruction from this site. In comparison with previous instructions this instruction has new section:
[CMD="cmd"]zfs create -V 4G zroot/swap
zfs set org.freebsd:swap=on zroot/swap
zfs set checksum=off zroot/swap[/CMD]

And I have several questions about swap on zpool. 

Is it useful to create swap on each zpool? For example I have legacy zpool where my system installed. And I import another zpool containing only my media data. Does media zpool need a swap too? Or it will be useless?
Which size it should have. Traditional swap partitions have size computed by $YOUR_RAM*2. Does this expression suitable for zfs swap?

I searched articles about zfs swap but did not find any complete and clear description..


----------



## AJ (Aug 2, 2012)

FreeBSD handbook suggests your swap size should be double your RAM, I'm not 100% sold on this, but because this is FreeBSD.org I'll reiterate what the handbook says


----------



## Crivens (Aug 2, 2012)

Not entirely true.
The system does not _need_ swap space, but it can profit from swap space.
Swap space may then be used to hold modified memory which was not touched for some time and which can now be used to cache other things. When swap space was not available, cache size would shrink.

When no swap space would be configured you could only have as much memory modified as you have real memory, minus some for kernel and buffers.

In any case, when memory is completely full and no more can me made free (by dropping cached memory or paging out some), the kernel would start killing processes. If that still is the case, I can not tell. 

Usually I start with half the memory size as swap and watch it when running some workloads. When it becomes more than about half full, I increase it.

As with 'normal' swap partitions it is good to distribute swap space over some disks so the kernel can load balance the I/O some. Because if swap space is hit, it needs to do this as fast as possible.


----------



## kpa (Aug 2, 2012)

As for the recommended size of swap, you can get away with very little swap unless you plan on doing kernel debugging that may require a full dump of the system memory. If you have let's say 4GBs of physical memory and you know you will almost never exhaust the physical memory completely as little as 512MBs of swap will do fine. 

The 2 times of physical memory rule doesn't apply to FreeBSD, it's a persistent myth based on systems where swap neeeded to be at least the size of physical memory to work because of very different implementation.


----------



## SirDice (Aug 2, 2012)

kpa said:
			
		

> The 2 times of physical memory rule doesn't apply to FreeBSD, it's a persistent myth based on systems where swap neeeded to be at least the size of physical memory to work because of very different implementation.




```
You should typically size your swap space to approximately 2x main memory
     for systems with less than 2GB of RAM, or approximately 1x main memory if
     you have more.  If you do not have a lot of RAM, though, you will gener-
     ally want a lot more swap.  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 ver-
     sus 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.  Finally, on larger systems with multiple
     SCSI disks (or multiple IDE disks operating on different controllers), we
     strongly recommend that you configure swap on each drive.  The swap par-
     titions on the drives should be approximately the same size.  The kernel
     can handle arbitrary sizes but internal data structures scale to 4 times
     the largest swap partition.  Keeping the swap partitions near the same
     size will allow the kernel to optimally stripe swap space across the N
     disks.  Do not worry about overdoing it a little, swap space is the sav-
     ing grace of UNIX and even if you do not normally use much swap, it can
     give you more time to recover from a runaway program before being forced
     to reboot.
```
Taken from tuning(7).


----------



## kpa (Aug 2, 2012)

For heavily loaded server systems that will swap that advice is spot on, I'm not disputing that. On desktop use you can get away with no swap at all in some cases without any ill effects.


----------



## SirDice (Aug 2, 2012)

I still tend to use the "2 times memory" rule. Harddrive space is cheap and to not let it all go to waste I use tmpfs(5).


----------



## gkontos (Aug 2, 2012)

For ZFS on root servers I allocate 1/2 of memory for swap. While using ZVOL for swap has proven to be safe, especially after FreeBSD9.0-RELEASE, I tend to use SSD's for that purpose since I also like to have some space for L2ARC.

IMHO full ZFS servers have much better memory management than UFS.


----------



## gkontos (Aug 2, 2012)

SirDice said:
			
		

> I still tend to use the "2 times memory" rule. Harddrive space is cheap and to not let it all go to waste I use tmpfs(5).



Memory is cheap also these days. I recently installed a system with 32GB of RAM with a 16GB failover (apache & mysql). 

SWAP is 16GB on primary and 8GB on failover. With very low utilization...

CACHE is 100 GB with 70% L2ARC efficiency.


----------



## Bront_in_Hood (Aug 3, 2012)

Thanks to everybody! I'm so pleased then so much people ready to help!
But I would appreciate some explanation for assertion that each zpool should have a swap...


----------



## SirDice (Aug 3, 2012)

gkontos said:
			
		

> Memory is cheap also these days.


True, but regarding swap space we're usually talking about less than a few percent of the total drive space.


----------



## gkontos (Aug 3, 2012)

Bront_in_Hood said:
			
		

> Thanks to everybody! I'm so pleased then so much people ready to help!
> But I would appreciate some explanation for assertion that each zpool should have a swap...



There is no reason for that. You will not benefit unless your system uses SWAP a lot.


----------



## xibo (Aug 5, 2012)

IMO there is no reason at all to use multi-pooled swap. If a system uses excessive amounts of swap I would rather consider to create a dedicated swap pool (of mirrored stripes) instead of causing extra load to the data pools.
Also, a modern processor can evaluate fletcher4 check-sums for gigabytes per second, while storage media is slower - therefore I see no point in disabling checksumming for swap (quite the opposite, since a rotten swap bit is almost certainly going to panic the system).


----------

