# Swap Space



## falkman (Jul 12, 2011)

Hello,

I generally follow the 2x RAM as a rule of thumb for my swap partition size. Recently I have upgraded my computer and I now have 24GB of RAM. I install BSD on my primary hard drive, which is a 50GB SSD. As you can see, I do not have enough room to even have 2x my RAM size in swap. I also have a platter disk for videos/music/etc which is 1TB. I'm just trying to figure out what would be most reasonable for swap space. Usually I just have 4GB of swap space on the SSD as I feel I'll never need to touch it with 24GB of ram in the first place.

I'm just looking for opinions and suggestions.

Thanks,
Brandon


----------



## graudeejs (Jul 12, 2011)

Swap = 2xRAM is bullshit (forget about it)
I have 8GB ram, I keep 1GB swap, at any time, I've never seen more than 100MB swap used (even on VERY HIGH load and very demanding RAM programs (such as many VirtualBoxes))

Even my old server (2.5G ram, + some swap) rarely use swap, not to mention that it runs zfs and 2 jails ATM

EDIT:
if you had 64MB ram, then it makes sense to add 128MB swap (especially if you have many discs)


----------



## gkontos (Jul 12, 2011)

killasmurf86 said:
			
		

> Swap = 2xRAM is boolshit
> I have 8GB ram, I keep 1GB swap, at any time, i've never seen more than 100MB swap user (evern on VERY HIGH load and very demanding RAM programs (such as many VirtualBoxes)



+1

Never gone more than 2GB. Btw. this rule of thumb applied in the 80's maybe early 90's when memory was really expensive.


----------



## graudeejs (Jul 12, 2011)

The only time my system used more swap, was when I wrote some app with bug, that allocated more ram over and over again.
Just for fun I watched my swap usage raise to 1GB, but I couldn't stop it. PC was absolutely unresponsive.
At About 1-1.5GB swap used I did hard reset and fixed that bug. It was interesting experience.


----------



## Oxyd (Jul 12, 2011)

killasmurf86 said:
			
		

> The only time my system used more swap, was when I wrote some app with bug, that allocated more ram over and over again.
> Just for fun I watched my swap usage raise to 1GB, but I couldn't stop it. PC was absolutely unresponsive.
> At About 1-1.5GB swap used I did hard reset and fixed that bug. It was interesting experience.



A memory allocation bug like that can bring FreeBSD to a halt fairly easily. I've had that happen as well. If you wait long enough, the system will kill the process and stuff will return back to normal. 

I still think it's kinda sad that a simple program can effectivelly bring the whole system down. Needless to say, I've managed to bring down various systems with this bug: FreeBSD, Linux and Windows 7 are all guilty of not handling this situation properly, if you let the process run long enough.


----------



## poh-poh (Jul 12, 2011)

I use 40G swap, mostly for use with tmpfs(5) - faster than ufs/zfs with a lot of small files. OTOH, if you have more than 1G ram it's probably better do away with swap at all. For a runaway process it'd only make system slow for while before being killed due to out-of-memory condition.


----------



## falkman (Jul 12, 2011)

Sounds good. I'm now leaning towards 1GB of swap instead of 4GB as I doubt I'll ever use all 24GB of ram. I feel that if I do manage to use all 24GB it would be due to a runaway program eating up memory like biscuits. Odds are that program wouldn't magically stop using ram, it would go through all the swap no matter how much. The only issue "The kernel can handle arbitrary sizes but internal data structures scale to 4 times the largest swap partition." -Handbook. I doubt I need massive data structures, and maybe I'm reading this line wrong. But it sounds like some data structure sizes are determined by the swap size. I'm sure I could overwrite the sizes anyway with some config or kernel hacking. Any reports on this?


----------



## graudeejs (Jul 12, 2011)

Oxyd said:
			
		

> A memory allocation bug like that can bring FreeBSD to a halt fairly easily. I've had that happen as well. If you wait long enough, the system will kill the process and stuff will return back to normal.
> 
> I still think it's kinda sad that a simple program can effectivelly bring the whole system down. Needless to say, I've managed to bring down various systems with this bug: FreeBSD, Linux and Windows 7 are all guilty of not handling this situation properly, if you let the process run long enough.



You can prevent this with /etc/login.conf... setting *memoryuse* and others. But for system it's hard to guess how much memory your apps will need. Probably 2GB might be good limit. (till this day 2GB was most ram I assigned to Virtual Machine)


----------



## ikreos (Jul 12, 2011)

On my system I have 4GiB of ram and 4GiB of swap. Even after weeks of up-time with multiple high memory programs running I have only ever seen my system use a max of maybe 1MiB of swap. Now if your running say NetBSD on an Amiga 1200 with 4MiB of ram, yeah your going to want 2-3x swap. On a modern system though, don't bother. Of course it would be different on a system that needs a lot of memory and swap.


----------



## falkman (Jul 12, 2011)

swap is always such a hard decision... I'm thinking 4GB, 1GB, perhaps 8GB, or maybe with 24GB ram no swap at all. The last (no swap at all) sounds pretty plausible on a system with 24GB, but it seems that it's usually expected to have swap so not having it could lead to odd behaviours.


----------



## graudeejs (Jul 12, 2011)

falkman said:
			
		

> swap is always such a hard decision... I'm thinking 4GB, 1GB, perhaps 8GB, or maybe with 24GB ram no swap at all. The last (no swap at all) sounds pretty plausible on a system with 24GB, but it seems that it's usually expected to have swap so not having it could lead to odd behaviours.



No weird things should happen. I was running my system without swap in past. Now after this thread I reevaluated my swap usage and decided to run my system without swap again (for some time).

Also What are you going to do, that you expect that with 24GB ram you will need swap?
Are you going to render The Matrix on your FreeBSD box? (10000 parallel rendering processes? Joking)


----------



## UNIXgod (Jul 12, 2011)

The rule was originally 2 to 3 times swap. initially the 3 times was in case of upgrading ram. I assume a system with 24gb of ram has multi tb drives attached. unless your on all ssd I can't imagine a system with less than 360gb in that setup. 48gb-72gb swap is trivial even if it's just buying insurance for unlikely exponential growth.

Then again this has always been a controversial topic where each admin may have a different policy for how to deal with swap.


----------



## falkman (Jul 12, 2011)

Alright. No swap it is then! I can't wait to get my new motherboard tomorrow. My current one's PCI has failed so there is no lan and my USB wireless chip is garbage. It also fails to boot FreeBSD as the kernel seems to deadlock (haven't confirmed via debugging) somewhere during boot.

Edit: New post above. I have a 50GB SSD and a 1TB media platter (internal ofc). I could sacrifice some of that 1TB as all I have on there are about 500GB of VMs. I guess I'll just do more and more research and find what I want to do. Normally I'll use 20GB on VMs tops. Liked mentioned earlier, the only way I'd use all 24GB is if there is a fault in the program that constantly allocates data. If that is the case, swap wont protect me at all anyways.


----------



## ikreos (Jul 12, 2011)

Also a lot of ram being used is for filesystem cache (at least on my system it is) which can be reclaimed easily. Another thing is if you go without swap and later find out you need swap, you could just create a swap file. I use tmpfs a lot too. Mainly for firefox cache, all the non persistent tmp mount points, as my mount point for building the system sources and ports. I do a lot of intensive stuff at once too (right now calculating 2 mil+ hex digits of pi using a python script), movies, compiling code, web browsing, graphics editing. All this is being done with 4GiB of ram and swap not being touched.

@killasmurf86 lol at The Matrix reference.


----------



## jalla (Jul 13, 2011)

If you ever want to capture a crashdump you need a swappartition at least the size of your RAM.


----------



## bes (Jul 13, 2011)

It can be not only partition ,it is possible to create a file of a specified size to use as a swap file , when you want it , and get rid of it when it is no longer needed.
Handbook Swapfiles


----------



## ahavatar (Jul 13, 2011)

I don't know about FreeBSD specifically, but some OS'es use swap partition to save RAM contents for hibernation. If the swap size is too small, hibernation doesn't work.


----------



## falkman (Jul 13, 2011)

I guess I'll just follow the standard RAM * 2 protocol here. Since that protocol has been around for a long time, I feel that somewhere out there things are optimized (whether you think that's right or wrong) for a system which has a design like that. I can easily sacrifice 48GB on my platter, so I'll have 4GB on my SSD for swap, then 44GB on my platter for swap. This should lead to the best performance. As mentioned above, I forgot about crashdumps. As a kernel dev myself, I find it critical to be able to get proper crashdumps, so that's what's making me go forth and add swap.


----------



## fwaggle (Jul 13, 2011)

It was explained to me on IRC that no swap is not as great an idea as it sounds. I asked about this, because on for example web-servers, if it starts paging stuff out, performance is going to hell anyway: the goal is to never hit swap.

But your system will have stuff that's allocated and then never touched (I think the example given to me was getty): eventually that stuff will get paged out and never paged back in, giving you a little bit more RAM than you'd have if you went completely swap-less. That may or may not be an issue for you.

Also, as said above, you need swap >= RAM if you want to get crash dumps. No one ever plans on crashing, but I do sometimes outgrow a filesystem or two so I like to keep a little unallocated space on a disk just in case, so that may not even be an issue.


----------



## poh-poh (Jul 13, 2011)

jalla said:
			
		

> If you ever want to capture a crashdump you need a swappartition at least the size of your RAM.


No longer true: minidump and textdump(4) take less space.


----------



## graudeejs (Jul 13, 2011)

jalla said:
			
		

> If you ever want to capture a crashdump you need a swappartition at least the size of your RAM.





			
				fwaggle said:
			
		

> Also, as said above, you need swap >= RAM if you want to get crash dumps. No one ever plans on crashing, but I do sometimes outgrow a filesystem or two so I like to keep a little unallocated space on a disk just in case, so that may not even be an issue.



You both are speaking about kernel crash?
For me It never happened to dump anything... after kernel crash... (Ye I didn't probably have swap=2xRAM)

But hat about minidumps? Never see any, don't even know what I'm talking about. But as I understand, the whole point was to get rid of that silly stupidity of keeping so much swap just to get kernel rash dump, who nobody will ever want to analyze (especially if its size is in GBs)


----------

