Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a server with Dual Nehalem Quad Core Xeon 5520 and 12GB DDR3 RAM. The server's average memory usage is only around 10-15%, yet the Swap used shows 10%+. Is this normal, or could something be wrong that is causing this. I was under the impression that Swap was only used if there wasn't enough memory available.

I am running Apache/2.0.63 on CentOS 5.3.

share|improve this question
    
how big is 10% of your swap? –  Alex Jurkiewicz Jun 10 '09 at 6:28
add comment

4 Answers

up vote 5 down vote accepted

There's nothing wrong with this. The kernel will page into virutal memory gradually over time. This can also occur if your server has activity "spikes" and the memory you have currently free quickly fills up. The pressure on the memory system causes page-outs to occur until the pressure is reduced to a set point (which you can examine in /proc/sys/vm). Even on a fairly idle system, I have seen gradual page-outs over time. So, unless the swap is fairly active (lots of page faults resulting in paging activity) I wouldn't worry about this.

If you're really that concerned, you can always turn swapping off, then on. This will force those swapped pages back into memory. I don't recommend doing this, but you can if you want. Just be sure that you have sufficient free memory before you do this.

share|improve this answer
add comment

You shouldn't worry about this - swap will be used if the kernel deems that the data is not going to be accessed enough to warrant having it in the physical memory.

If you'd like to monitor "bad" swap usage, you're better off monitoring the rate at which swap reads and writes are occurring, not simply if it's being used at all or not.

For example...

# vmstat 3
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 1  0  19528  45324  19348 240132   50   17    93    26   30   30  0  1 98  1
 0  0  19528  45324  19348 240156    0    0     0     3    9   23  0  0 100  0
 0  0  19528  45324  19352 240156    0    0     0     9    9   25  0  0 100  0
 0  0  19528  45324  19352 240156    0    0     0     0    5   17  0  0 100  0

The first report tells you what the states (average) are since the machine was first brought up, the rest should you (in the case above) 3-second averages.

Notice the si and so fields are mostly zero - that's what you're interested in.

share|improve this answer
add comment

That is fine, as already pointed out usage spikes may cause this or the kernel may decide that some items are put into swap because it's smarter to use the RAM for something else (page cache).

As answers to other questions pointed out sysctl vm.swappiness may be of interest to you do avoid this.

share|improve this answer
add comment

(posted as an answer, as it got far too long to add as an additional comment to Avery Payne's answer)

Also, if swap has been used during a peak period which has since passed you may find that most of the data in swap is also currently in RAM. If Linux reads pages back from swap in to RAM it doesn't immediately deallocate the swap space unless it needs it for more data, that way if it needs to swap the pages back out (and it knows that the data in them has not changed) it doesn't actually need to write the pages to disk as they are already there.

See /proc/meminfo for details. On a little server of mine currently:

olm:/proc# free -m
             total       used       free     shared    buffers     cached
Mem:          1487       1457         30          0         15       1094
-/+ buffers/cache:        347       1139
Swap:          980        112        868
olm:/proc# cat meminfo
MemTotal:      1523572 kB
MemFree:         30688 kB
Buffers:         15724 kB
Cached:        1120884 kB
SwapCached:      67868 kB
SwapTotal:     1004052 kB
SwapFree:       888928 kB

So here ~66Mb of the 112Mb of swap space allocated is also currently present in RAM. There is no point it removing that 66Mb from swap as there is no demand for the space for other uses (there is plenty of completely free swap space). If swap gets full those pages will be reallocated, if the pages change in RAM they will be marked as dirty and free to be reallocated, but if they need to be swapped back out the kernel can save itself a bunch of disk writes.

If I force the disk caches and buffers to be cleared with

sync; echo 3 > /proc/sys/vm/drop_caches

the result stays the same:

olm:/proc# free -m
             total       used       free     shared    buffers     cached
Mem:          1487       1278        209          0          0        979
-/+ buffers/cache:        298       1189
Swap:          980        112        868
olm:/proc# cat meminfo
MemTotal:      1523572 kB
MemFree:        212320 kB
Buffers:           652 kB
Cached:        1005732 kB
SwapCached:      67868 kB
SwapTotal:     1004052 kB
SwapFree:       888928 kB

The "cached" reading stayed high after asking for the disk caches to be cleared as this is counts memory allocated to VMWare VMs too because of the way that is allocated. The SwapCached reading didn't alter as there is no point copying the pages back into RAM just because the RAM is now free - they might never be needed before the RAM gets allocated for something else again so those reads would be wasted.

The situation above is slightly different to yours in that this machine pretty much always has all its RAM allocated to something (VMs, other processes, I/O cache+buffers) but depending on your machine's load history since last boot it is not unlikely that a chunk of the pages in allocated space in your swap area are similarly also in RAM.

share|improve this answer
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.