Swap 相关内容
General
In some cases it is good for the swap partition to be used. For example, long running processes often access only a subset of the page frames they obtained. This means that the swap partition can safely be used even if memory is available because system memory could be better served for disk cache to improve overall system performance. In fact, in the 2.6 kernel used in Red Hat Enterprise Linux 4 and 5, you can define a threshold when processes should be swapped out in favor of I/O caching. This can be tuned with the /proc/sys/vm/swappiness kernel parameter. The default value of /proc/sys/vm/swappiness is 60 which means that applications and programs that have not done a lot lately can be swapped out. Higher values will provide more I/O cache and lower values will wait longer to swap out idle applications. Swappiness percentage may be tuned using:
1 | cat /proc/sys/vm/swappiness |
Depending on your system profile you may see that swap usage slowly increases with the time the system is up. To display swap usage you can run the free(1) command or you can check the /proc/meminfo file. When the system uses swap space it will sometimes not decrease afterward. This saves I/O if memory is needed and pages do not have to be swapped out again when the pages are already in the swap space. However, if swap usage gets close to 80% - 100% (your threshold may be lower if you use a large swap space), then a closer look should be taken at the system, see also Section 6.2, “Checking Swap Space Size and Usage”. Depending on the size of your swap space, you may want to check swap activity with vmstat or sar if swap allocation is lower than 80%. But these numbers really depend on the size of the swap space. The vmstat or sar command output the number of pages swapped. This output field is an important metric. This number should be low or zero as constant page swapping should be avoided at all costs.
Swap
You can check the size and current usage of swap space by running the command:
1 | grep SwapTotal /proc/meminfo |
Swap usage may slowly increase as shown above but should stop at some point. If swap usage continues to grow steadily or is already large, then one of the following choices may need to be considered:
Adding more RAM.
Reducing the size of the SGA.
Increasing the size of the swap space.
If you see constant swapping, then you need to either add more RAM or reduce the size of the SGA. Constant swapping should be avoided at all cost. You can check current swap activity using the following commands:
1 | vmstat 3 100 |
1 | ls -al /var/log/sa | grep "Oct 12" |
The fields pswpin and pswpout show the total number of pages brought in and out per second, respectively.
If the server shows sporadic swap activity or swap activity for a short period time at certain intervals, then you can either add more swap space or RAM. If swap usage is already very large, do not confuse very large swap usage with constant swapping, then more RAM is recommended.