5.1. What is Swap Space?
Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.
Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.
Swap should equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB.
So, if:
M = Amount of RAM in GB, and S = Amount of swap in GB, then
If M < 2 S = M *2 Else S = M + 2
Using this formula, a system with 2 GB of physical RAM would have 4 GB of swap, while one with 3 GB of physical RAM would have 5 GB of swap. Creating a large swap space partition can be especially helpful if you plan to upgrade your RAM at a later time.
For systems with really large amounts of RAM (more than 32 GB) you can likely get away with a smaller swap partition (around 1x, or less, of physical RAM).
Recommended System Swap Space
Amount of RAM in the System | Recommended Amount of Swap Space |
---|---|
4GB of RAM or less | a minimum of 2GB of swap space |
4GB to 16GB of RAM | a minimum of 4GB of swap space |
16GB to 64GB of RAM | a minimum of 8GB of swap space |
64GB to 256GB of RAM | a minimum of 16GB of swap space |
256GB to 512GB of RAM | a minimum of 32GB of swap space |
To check The Total Swap,Physical &buffers/cache Space in System
free -m
total used free shared buffers cached
Mem: 3953 315 3637 8 11 107
-/+ buffers/cache: 196 3756
Swap: 0 0 4095
1. Creating Swap Partition
To add an extra swap partition to your system, you first need to prepare it. Step one is to ensure that the partition is marked as a swap partition and step two is to make the swap filesystem. To check that the partition is marked for swap, run as root:
fdisk -l /dev/hdb
Replace /dev/hdb with the device of the hard disk on your system with the swap partition on it. You should see output that looks like this:
Device Boot Start End Blocks Id System
/dev/hdb1 2328 2434 859446 82 Linux swap / Solaris
If the partition isn't marked as swap you will need to alter it by running fdisk and using the 't' menu option. Be careful when working with partitions -- you don't want to delete important partitions by mistake or change the id of your system partition to swap by mistake. All data on a swap partition will be lost, so double-check every change you make. Also note that Solaris uses the same ID as Linux swap space for its partitions, so be careful not to kill your Solaris partitions by mistake.
2. Format new Partition Using Swap File system
Once a partition is marked as swap, you need to prepare it using the mkswap (make swap) command as root:
mkswap /dev/hdb1
3. Activate The New Swap Partition
If you see no errors, your swap space is ready to use. To activate it immediately, type:
swapon /dev/hdb1
3. Varify The Swap Partitions
You can verify that it is being used by running swapon -s.
swapon -s
4. Mount The Swap partition
To mount the swap space automatically at boot time, you must add an entry to the /etc/fstab file, which contains a list of filesystems and swap spaces that need to be mounted at boot up. The format of each line is:
- To enable the swap file immediately but not automatically at boot time:
swapon /swapfile
- To enable it at boot time, edit
/etc/fstab
to include the following entry:
The next time the system boots, it enables the new swap file./swapfile swap swap defaults 0 0
5. Add New Activated Swap space To Old Swap
swapon -v /dev/hdb1
6. Deactivate swap Partition
Sometimes it can be prudent to reduce swap space after installation. For example, say you downgraded the amount of RAM in your system from 1 GB to 512 MB, but there is 2 GB of swap space still assigned. It might be advantageous to reduce the amount of swap space to 1 GB, since the larger 2 GB could be wasting disk space.
swapoff -v /dev/hdb1
0 comments:
Post a Comment