Does swap file usage increase disk I/O and CPU load?

Someone brought to my attention that if an application is using swap file (when system RAM is very low) it increases the disk I/0 and as a result of that, CPU use % and CPU Load increases.

Is this true or was that guy completely off-track?


Solution 1:

Swap is paging memory to disk. If you've got something that's shunting things into swap from RAM, you're going to see:

  • CPU use for determining what can go into swap
  • Disk IO and CPU while moving from physical to swap
  • CPU use for mapping running applications address space to its swap location
  • Disk IO from application accessing their swapped memory space.

How quantifiable these are or how much of an effect they have is completely debatable but follow Barney's one golden rule: More RAM is always better.

Solution 2:

The Swap space is the area on the HDD or SSD which forms a part of the Virtual Memory of your PC. The Virtual Memory is simply a combination of your physical memory which is called RAM and the swap space which is call SWAP. The Swap space holds, temporarily, the memory that is inactive. This is because your system decides that it needs physical memory for active processes and it has insufficient unused physical memory available. If the system happens to need more memory or space, inactive memory pages in the physical memory are then moved to the swap space, freeing up that physical memory for other uses.

Since physical memory or RAM is much faster than SWAP, which is found in a HDD or SDD, typically as a partition, the access time is slower. You can however get a faster speed if you use a SSD (Solid State Drive) instead of a HDD (Hard Disk Drive, Mechanical ones).

With this in mind, the answer to your question is, in both cases yes. Yes, using SWAP will increase your CPU usage and the Disk I/O. Of course the CPU usage will be minimal, but the Disk usage won't be. The reason is that the CPU is only used to see where and what can be stored/retrieved from swap (which is a very fast procedure).

For the Disk I/O it's different. Since Swap is in the HDD or SSD, it will also use the Disk I/O while you use other programs, and it will most likely have higher priority than other user programs.

So for your question, you do not need to worry much about CPU usage than Disk usage.

To enhance performance and lower disk usage you can do a couple of things:

  • Change from an HDD (Typically 5400 RPM to 7200 RPM) to an SSD. The difference is 2x to 10x faster Disk performance. This will drastically change the overall performance on your system.

  • Add more RAM. If you have more memory RAM, the chance the system will need to swap memory will be lower, increasing the overall performance while still using all available RAM.

  • Use a Desktop that uses less RAM. This also expands to other apps. It is not the same to use KDE or Gnome than to use LXDE or XFCE. It is also not the same to use abiword or even nano o vim than to use LibreOffice.

Now for the last point. What you heard about Increased Disk I/O increasing CPU load. This is true, simply because:

The less RAM you have, the more the SWAP space will be needed for your Apps. The more SWAP space is used, the more CPU time will be needed to store/retrieve Swap information.

One depends on the other. So increasing the amount of RAM will directly benefit you in lowering SWAP usage and lowering CPU usage.

Here is an image to illustrate the effect:

enter image description here

Every time the CPU load jumped, it was because the Swap was been read/written to.