6

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?

4 Answers 4

7

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.

3
  • 1
    Nice HIMYM reference there ;)
    – Novine
    Oct 29, 2013 at 15:21
  • 1
    And note that if you need to swap, but don't/can't, then it's much, much worse... Linux will kill something trying to kill the "less used" thing [the magic of this varies depending on which kernel you use], but with quite a good chance to kill something that you (or some program, or daemons) actually need... Oct 29, 2013 at 18:46
  • And once you swap out something not really used, it frees up disk and cpu for the things you actually are using now... so it's a "little" time "lost", for a lot of time gained. Oct 29, 2013 at 18:48
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.

5
  • storing and retrieving swap takes disk IO, not cpu time.
    – psusi
    Oct 30, 2013 at 13:45
  • 1
    @psusi It takes both. Even if the amount of CPU is minimal, it still takes CPU. Oct 30, 2013 at 13:50
  • Technically, but the amount is not going to show up on a cpu graph like that as it is measured in nanoseconds per swap. Being swap bound is about as far away as you can get from being cpu bound.
    – psusi
    Oct 30, 2013 at 13:56
  • Try the following, HDD 5400rpm (One of those 160GB ones) and a PC with 512 MB DDR or DIMM. Let me know if spikes do not appear when you pass the physical amount of RAM. Oct 31, 2013 at 0:47
  • The cpu usage spikes because those are the moments where the process can actually run because it isnt blocked on IO. Without swapping, you would have the same cpu total, just all at once instead of with big gaps in between where it's blocked. In other words, those spikes are not because of the swapping, it is the troughs that are caused by the swapping.
    – psusi
    Oct 31, 2013 at 2:55
0

There will some SOME increase (as the kernel has to track the location of the paged memory between RAM and secondary storage), but I doubt it will be much as the kernel accesses the disk directly , bypassing caching and file-system when paging in/out of the swap file/partition. The IO performed is asynchronous, so the CPU is not waiting (causing other processes to slowdown unnecessarily).

For a full description of the swap management read https://www.kernel.org/doc/gorman/html/understand/understand014.html you will see that it is not a CPU intensive process, most of the work is around managing page mappings, not the transfer of data.

0

It is the reverse actually: when programs have to stop and wait for disk IO, cpu usage drops ( since they aren't using it while waiting ). Obviously lots of data being swapped in and out to disk is disk IO.

3
  • disk is disk IO - apart from you have 2 types, sync and async ;). Swapfile IO is asynchronous so the CPU does not have to wait.
    – NGRhodes
    Oct 29, 2013 at 14:40
  • 1
    NGRhodes: not in principle, but while lots of data is being sent to swap the processor typically does go idle, probably when it needs to do memory allocations, which may be delayed until enough space is available in RAM. Programs allocating lots of memory is the main cause for increased swapping activity. Oct 29, 2013 at 17:26
  • 1
    @NGRhodes, swapping can be done async, but most of the time it is synchronous as the process waiting for the page to be swapped back in needs it before it can continue, thus it stops using the cpu until the page has been loaded.
    – psusi
    Oct 30, 2013 at 13:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .