Do we really need swap space or disk partition on modern Linux desktop or server system? Red Hat blog posted a good overview of when to use SWAP space and provides some recommendation for swap sizes for Linux server.
Swap is used to give processes room, even when the physical RAM of the system is already used up. In a normal system configuration, when a system faces memory pressure, swap is used, and later when the memory pressure disappears and the system returns to normal operation, swap is no longer used. In this typical situation, swap helped through the time of memory shortage, at the cost of reduced performance while swapping.
Pages that were stored in swap are not moved back into RAM unless they are accessed/requested. This is the reason that many counters show swapped pages, although the system might already be in a state where no active swapping is happening.
- Read more: Do we really need swap on modern systems?