I was wondering since I heard that swap is used when you're out or RAM and and swapping too much is bad, since I have a lot of RAM ( 8GB )....
what if I dont create a swap partition?
Also, do I need it for hibernation or it isn't a must?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
Modern operating systems require a swap space to make efficient use of RAM. Even if your system has plenty of RAM, wasting RAM results in a smaller buffer cache, which means increased disk I/O. So no matter how much RAM you have, you still want the system to use it efficiently. Using it efficiently means getting things out of RAM that are extremely unlikely to ever be accessed.
When you start up a typical system, a large number of services start up. Programs run initialization code and modify private memory mappings in the process. A number of these services will never run again. Many of them won't run for hours. Without swap, the OS has no choice but to keep the modified private memory mappings associated with those services in RAM forever. That's RAM that can never be used as disk cache.
So you want swap whether you need it or not.
I've been running a desktop system without any swap for a couple years, and it's been going great! There are a few behaviours that are different; some of these are advantageous and some might harm you. It all depends on what you're doing.
One major difference is how the system behaves when memory runs out:
If there is no swap partition, the OOM killer runs immediately. If you've got a program leaking memory, that's likely to be the one that gets killed. That happens and you recover the system nearly instantly.
If there is a swap partition, the kernel pushes the contents of memory into swap. Errant process can continue allocating memory. This keeps happening until swap runs out, which could be ten minutes to an hour if you have a big swap partition. While it's doing this, your system slows to a crawl. It becomes impossible to open a terminal and kill the process. I usually unplug the power cord when this happens.
So, since I'm going to lose my data anyways when the system OOMs, I prefer the former option to at least have a (high) chance of recovery.
It is a common misconception to think having a swap area negatively affects performance. What does severely affects performance is not having enough RAM. Swap area per se have no negative effect on performance unless you do not care about reliability. It can have a positive impact on performance even while you think you have enough RAM (and no RAM shortage is reported).
There are essentially three cases to consider:
1: There is enough RAM for internal kernel needs, for all your applications to have their working set of pages on RAM and still have "free" RAM for the buffer cache to store most of the file system hot data.
2: same as the above except you have not enough free RAM for the buffer cache to be fully efficient.
3: There is not enough RAM to even store your applications used pages.
In case 1, which should be the norm for a production machine, having a swap area or not doesn't change anything (at least on Linux based OSes and other OSes that over-commit memory).
In case 2, having a swap area might improve the overall performance by allowing the system to page out very infrequently used pages and then allow the buffer cache to better play its role.
In case 3, having a swap area allows application to keep on running at the expense of a performance degradation due to pagination. On the other hand, missing a swap area (or a large enough one) will make applications crash randomly. Additionally, depending on OS settings, the OOM killer might also decide to kill the most memory hungry applications if there is a strong RAM demand without even giving them a chance to save their (your) critical data.
This latter case is one with which you have to make a choice. There are certainly some use cases where having processes being killed is the preferred option like systems where response time is crucial and where losing a transaction / process is no big deal.
However, I tend to believe in most situations, it is better to be able to let a chance for the user/administrator to notice the RAM shortage and act accordingly without risking losing data.
If you don't want to use a swap partition, you can use a swap file: a filesystem image stored on a regular file and used as your swap space.
The following articles explain how to do it in detail:
It's a bit dangerous but you CAN run without a swap space. But if you finally exceed the amount of memory you have your system will nearly immediately crash without notice.
What swap gives you basically is an extended but far slower for your memory. If you exceed you will start swapping, it will really take the life out of your system....HOWEVER by killing off the errant processes you can still save your system.
Some programs insist on allocating (but not using) swap space, some of these are database programs if I recall. Again, by not having swap space you might limit the programs that may be run.
Finally, look SWAP is CHEAP in the fact that you do not have to backup the area. (I'm hoping that you are one of these fine outstanding sysadmins who backup their systems).
So create it.
My rule of thumb used to be 2*memory-size.....but now adays I am ok in many cases with 1*memory-size but generally use 1.5*(memory-size). You don't have to create this great of an amount, but I would...especially if there is any production type of work you are doing.
Oh yes, and for hibernation, swap is REQUIRED to hold a total image of memory. And thus the size if you are considering h8ibernation should be AT LEAST (1*memory-size)+100MB..the 100 MB is for possible overhead needed in the process.
Linux swap seems to have quite a hard requirement as soon as there is memory pressure. The symptom is that if almost all RAM is used by active processes, the system stalls hard and the harddisk is running wild.
Pages without a backing file (usually from dynamic memory allocation) can't be evicted from RAM when there is no swap. The kernel resorts to pages with a backing file even if they are actually needed soon again, i. e. trashing even without swap!
Conclusion: Always have a swap.
And for the size as a rough guideline use something between 4 and 8 GB, even for systems with a lot of RAM, assuming you don't hibernate. For more details see Red Hat's documentation about swap space.