When is swap memory useful? Why do we need to set it to twice the amount of RAM? [duplicate]
Solution 1:
The swap space is usually used as a replacement for RAM, as in another storage for pages which are in memory, but not necessarily only when you run out of RAM, since RAM too as other usage than being memory for your programs: it is common for modern memory managers to move the memory allocated by programs which are idle in background into swap space and use the newly freed RAM space to cache disk accesses.
Another usage of swap space is to save a snapshot of RAM when doing suspend to disk or hibernate (so that the system can shut down the power from RAM without data loss).
Having swap double than RAM makes sense because, well, if you bought that much ram is because you know you'll need it, so you'd better have a proportional swap space ready as well because you will likely need that too. In modern system it is sufficient to have enough swap space to keep out of the way background idle services which would otherwise clobber the RAM you would use for other more important stuff (unused RAM can always be exploited to cache disk anyway, which speeds up your system A LOT). Some system save memory pages into swap preventively while they are still in RAM, so that if they need to remove them to free a bit of memory they can do it a lot faster (they can be discarded without the need to write them on disk because they are already there). For this reason (and to allow hybernation) it is probably a good idea to have at least enough swap space as the amount of RAM, except for cases in which you have an exceptionally huge amount of RAM and/or a small disk (like SSD drives)
While idle processes' memory can be swapped out, this is not true for active processes: when a process requires a page which has been swapped out, it triggers what is called a page fault: the process is paused, memory manager gets the page back into RAM, then the process is resumed and can read/write the required memory. This means that if your working set (i.e., the data which is currently being accessed frequently, by non-idle applications) is bigger than available RAM, you will need to swap in and out pages almost at every memory read or write, making the system so slow that it becomes unusable (this is called thrashing). So the swap space cannot actually substitute the RAM you are actually using, only the one your processes allocated but they are not really using.
Solution 2:
In general, memory is swapped out when you are running out of available memory on the computer.
What basically happens is that the operating system will store some of the contents of the RAM onto the storage drive to make room for other applications' memory needs. How the operating system chooses what to take off of the RAM depends on when it was last accessed. The available space for the operating system to use for swapping is called virtual memory.
The rule of thumb is to have your swap be twice the amount of RAM installed in your computer. However, this may not be the best for you. If you have 2GB of RAM installed, your swap size will be 4GB of virtual memory, giving you a total of 6GB of memory to work with. While 6GB may work for most people, this may not be enough for you.
The downside of swapping memory is that it takes up space on your storage drive. Because it is on the storage drive, accessing information on it will be noticeably slower than if it was on the RAM.
You can get away with have little to no space reserved for your computer to swap on your storage drive. With or without this, if your computer has to remember more than the total memory (physical + virtual memory), it may become unstable. It is important that the computer has more than enough memory to work with.
The operating itself may have additional use for the space as well. For example, Windows. If Windows itself crashes, it saves a crashdump file to the space reserved for the swap. For this reason, I do not recommend completely getting rid of the swap space, but you should check to see what your operating system does with the space first.