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).
To add a swap file:
To add a swap volume group (assuming
To add a swap file:
For removing LVM2 for swap
The swap logical volume cannot be in use (no system locks or processes on the volume). The easiest way to achieve this it to boot your system in rescue mode. Refer to for instructions on booting into rescue mode. When prompted to mount the file system, select Skip.
To remove a swap volume group (assuming
To add a swap file:
- Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536.
- At a shell prompt as root, type the following command with
countbeing equal to the desired block size:dd if=/dev/zero of=/swapfile bs=1024 count=65536 - Setup the swap file with the command:
mkswap /swapfile - To enable the swap file immediately but not automatically at boot time:
swapon /swapfile - To enable it at boot time, edit
/etc/fstabto include the following entry:
The next time the system boots, it enables the new swap file./swapfile swap swap defaults 0 0 - After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command
cat /proc/swapsorfree.To extend an LVM2 swap logical volume (assuming/dev/VolGroup00/LogVol01is the volume you want to extend):- Disable swapping for the associated logical volume:
# swapoff -v /dev/VolGroup00/LogVol01 - Resize the LVM2 logical volume by 256 MB:
# lvm lvresize /dev/VolGroup00/LogVol01 -L +256M - Format the new swap space:
# mkswap /dev/VolGroup00/LogVol01 - Enable the extended logical volume:
# swapon -va - Test that the logical volume has been extended properly:
# cat /proc/swaps # free
/dev/VolGroup00/LogVol02 is the swap volume you want to add):- Create the LVM2 logical volume of size 256 MB:
# lvm lvcreate VolGroup00 -n LogVol02 -L 256M - Format the new swap space:
# mkswap /dev/VolGroup00/LogVol02 - Add the following entry to the
/etc/fstabfile:/dev/VolGroup00/LogVol02 swap swap defaults 0 0 - Enable the extended logical volume:
# swapon -va - Test that the logical volume has been extended properly:
# cat /proc/swaps # free
- Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536.
- At a shell prompt as root, type the following command with
countbeing equal to the desired block size:dd if=/dev/zero of=/swapfile bs=1024 count=65536 - Setup the swap file with the command:
mkswap /swapfile - To enable the swap file immediately but not automatically at boot time:
swapon /swapfile - To enable it at bo ot time, edit
/etc/fstabto include the following entry:
The next time the system boots, it enables the new swap file./swapfile swap swap defaults 0 0 - After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command
cat /proc/swapsorfree.To reduce an LVM2 swap logical volume (assuming/dev/VolGroup00/LogVol01is the volume you want to extend):- Disable swapping for the associated logical volume:
# swapoff -v /dev/VolGroup00/LogVol01 - Reduce the LVM2 logical volume by 512 MB:
# lvm lvreduce /dev/VolGroup00/LogVol01 -L -512M - Format the new swap space:
# mkswap /dev/VolGroup00/LogVol01 - Enable the extended logical volume:
# swapon -va - Test that the logical volume has been reduced properly:
# cat /proc/swaps # free
The swap logical volume cannot be in use (no system locks or processes on the volume). The easiest way to achieve this it to boot your system in rescue mode. Refer to for instructions on booting into rescue mode. When prompted to mount the file system, select Skip.
To remove a swap volume group (assuming
/dev/VolGroup00/LogVol02 is the swap volume you want to remove):- Disable swapping for the associated logical volume:
# swapoff -v /dev/VolGroup00/LogVol02 - Remove the LVM2 logical volume of size 512 MB:
# lvm lvremove /dev/VolGroup00/LogVol02 - Remove the following entry from the
/etc/fstabfile:/dev/VolGroup00/LogVol02 swap swap defaults 0 0 - Test that the logical volume has been extended properly:
# cat /proc/swaps # freeTo remove a swap file:- At a shell prompt as root, execute the following command to disable the swap file (where
/swapfileis the swap file):# swapoff -v /swapfile - Remove its entry from the
/etc/fstabfile. - Remove the actual file:
# rm /swapfile
No comments:
Post a Comment