Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a question regarding swap and physical memory. Often times I run a program that requires a lot of memory and as a result I can see some of the data is copied from the physical memory into swap. However, once the program is terminated, and the physical memory is freed I can still see a considerable amount of data on swap which significantly slows down the system and is annoying!

What is the reason behind this and how does the OS decide which part of data should go to swap? How long is this data supposed to be there and how is it "freed"?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

If you frequently experience slowdown due to page swapping you likely need more memory. Shutting down unused or low priority services and making other memory reducing adjustments may be used as a temporary stopgap.

What is the reason behind this and how does the OS decide which part of data should go to swap?

While your memory heavy process was running the memory management swapped out pages to make room for your process. This is usually done on a Least Recently Used basis. (Programs often have pages of memory which were required at one point, but no longer are.)

If you have multiple devices, you can break the page mapping by removing that swap partition. This will force the pages into memory (although they may be swapped out fairly quickly).

How long is this data supposed to be there and how is it "freed"?

Once mapped to swap, pages may remain mapped to swap until the process is terminated at which point the pages are "freed". Pages mapped to the swap space may be located in memory. The page manager may maintain the mapping to prevent the need to remap the page if it needs to be swapped out again.

Some slowdown while the pages are swapped into memory is to be expected. Depending on memory demand and program scheduling, use of swap in known to significantly decrease performance.

Using sar or munin to monitor page in/out activity should enable you do see how long it takes to map active pages into memory. Once they have been swapped back into memory they should stay there as long as memory is available.

Some things which may increase swap performance.

  • A dedicated swap partition located next to actively accessed partitions. This reduces head latency when swapping is required.
  • Using a swap file located on the most heavily used partition. This may reduce head latency.
  • A dedicated drive for the swap partition. A number of I/O performance improvements apply.
  • Use of an SSD for swap space. I/O is much faster than disk. (A fast USB drive may also perform better than your disk, but measure I/O rates first.)

Swap files and partitions can be added and removed from a running system. Swap devices can be given different priorities to control which device is used first.

share|improve this answer
add comment

I'm simplifying things but there are only two instances where paged memory in swap is copied back out to physical RAM:

  1. There's a page fault (app tries to call memory mapped to swap).
  2. You use sudo swapoff -a (et al) to turn swapping off.

Anything else tends to be a waste of time. Memory in swap tends to be relatively unused, low trafficked stuff. The algorithm Linux uses things going into swap is LRU (Least Recently Used). If you find yourself in a situation where you're getting disk trashing and applications are stalling on page faults, the problem is simple to fix: you need more RAM.

Memory just sitting in swap will not slow the computer. It only has that effect when something has to be stuck in swap and used.

Note: As I said, I'm simplifying things here. Memory management is a absolutely huge topic and my few paragraphs don't do the algorithms, their designers or their implementers any justice. I've had my own share of battles with Linux memory and cache management over the years but something I've learnt so far: the Kernel devs know more than me. They set things up the way they are for a reason.

share|improve this answer
    
+1 the Kernel devs DO know more than us mere mortals. It reminds me of Andrew Morton's comments years ago about people tuning swappiness settings: "I'm gonna stick my fingers in my ears and sing 'la la la' until people tell me 'I set swappiness to zero and it didn't do what I wanted it to do'." You have got to respect that: kerneltrap.org/node/3000 –  Richard Holloway Mar 27 '12 at 11:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.