Linux/Swap

From Omnia
Revision as of 14:13, 9 May 2016 by Kenneth (talk | contribs) (→‎Linux Swap Space)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Linux Swap Space

See active swap partitions:

swapon -s
cat /proc/swaps

Create swap space:

mkswap /dev/[device]
mkswap -L [label] /dev/[device]

Turn swap space on/off:

swapon /dev/[device]
swapoff /dev/[device]
swapon -a    # turn on all in /etc/fstab
swapoff -a   # turn off all in /etc/fstab

Swap usage:

free -m
top

Swappiness:

/proc/sys/vm/swappiness

/etc/fstab:

LABEL=/swap1        swap                  swap    defaults,pri=1 0 0
LABEL=/swap2        swap                  swap    defaults,pri=1 0 0
LABEL=/swap3        swap                  swap    defaults,pri=1 0 0

Swap in a file: # WON'T WORK ON NFS MOUNT!

dd if=/dev/zero of=/swap.img bs=1M count=1000
chmod 600 /swap.img
mkswap /swap.img
swapon -v /swap.img

Note: if you try to mount a swap file on NFS you will get this error: "swapon: /swap.img: Invalid argument"

How large should my space space be

Select right size for your setup.

Here is my rule for normal server (Web / Mail etc):

  1. Swap space == Equal RAM size (if RAM < 2GB)
  2. Swap space == 2GB size (if RAM > 2GB)

My friend who is a true Oracle GURU recommends something as follows for heavy duty Oracle server with fast storage such as RAID 10:

  1. Swap space == Equal RAM size (if RAM < 8GB)
  2. Swap space == 0.50 times the size of RAM (if RAM > 8GB)

Suspend

If you are going to suspend to disk, then you need swap space more than actual RAM. For example, my laptop has 1GB RAM and swap is setup to 2GB. This only applies to Laptop or desktop but not to servers.

References:

Swappiness

"The Linux 2.6 kernel added a new kernel parameter called swappiness to let administrators tweak the way Linux swaps. It is a number from 0 to 100. In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle. Kernel maintainer Andrew Morton has said that he runs his desktop machines with a swappiness of 100, stating that "My point is that decreasing the tendency of the kernel to swap stuff out is wrong. You really don't want hundreds of megabytes of BloatyApp's untouched memory floating about in the machine. Get it out on the disk, use the memory for something useful."

One downside to Morton's idea is that if memory is swapped out too quickly then application response time drops, because when the application's window is clicked the system has to swap the application back into memory, which will make it feel slow.

The default value for swappiness is 60. You can alter it temporarily (until you next reboot) by typing as root:

echo 50 > /proc/sys/vm/swappiness

If you want to alter it permanently then you need to change the vm.swappiness parameter in the /etc/sysctl.conf file."

Source: All about Linux swap space