- Swap space is hard disk space that extends
system RAM.
Swap space or virtual memory is hard disk
space that acts as an extension of system RAM. Of
course, due to the relative differential in data access on RAM versus hard
disk, we prefer not to use swap space if it can be avoided. Nonetheless, it is
vital to the proper functioning of a typical Linux system that some swap
space be made available.
Swap space 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 (a swap file can be used, provided the
space has been pre-allocated using a tool such as dd).
The size of your swap
should be equal to twice your computer’s physical RAM for up to 2 GB of
physical RAM. For physical RAM above 2 GB, the size of your swap should be
equal to the amount of physical RAM above 2 GB. The size of your swap should
never less than 32 MB.
Using this basic
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 Swap space As mentioned before, a swap space can be
a partition or a file with pre-allocated space.
Setting up a Swap file
Suppose your system
RAM is 2GB. Then you want to allocate 2 GB for swap space. So create a file
anywhere for example in / as /swapfile.
Step 1: Open a shell, login as root
Step
2: Use dd command
to create the file called /swapfile. We can use dd many
ways to create the file.
[root@server
~]# dd if=/dev/zero of=/swapfile bs=1k count=2M
This
will dump 2 million blocks of 1KB each into the /swapfile.
or
[root@server
~]# dd if=/dev/zero of=/swapfile bs=2k count=1M
This will dump 1
million blocks of 2KB each into the /swapfile.
or
[root@server
~]# dd if=/dev/zero of=/swapfile bs=1024 count=2097152
This will dump 2097152
blocks of 1024 each into the /swapfile.
Use any of these as you.
Step
3: Create
the signature using mkswap.
[root@server
~]# mkswap /swapfile
Step
4: Add the
swap space to the /etc/fstab file to make it persistent at
system reboots.
[root@server
~]# vim /etc/fstab
Add the following line
/swapfile
swap swap
defaults 0 0
Step
5: Activate the new
swap space using swapon -a.
[root@server
~]# swapon -a
Step
6: Check the swap
partition’s status using swapon -s or free -m.
[root@server
~]# swapon -s
[root@server
~]# free -m
Setting
up a Swap partition
Use fdisk or
some other partitioning program to add a partition. Set the partition id type
to 0×82 (Linux Swap / Solaris). Create the signature on the
partition using mkswap and set a signature label to identify
it consistently if disk device pathnames change.
Step
1: Open a shell, login
as root
Step
2: Open fdisk to
create the partition and change the system id.
[root@server
~]# fdisk -cu /dev/sda
Press
n to add a new partition.
Now
change the new partition's system id by pressing the t option.
Now save the table and
exit. Restart for it it to work properly.
Step
3: Create the signature
using mkswap.
[root@server
~]# mkswap -L SWAP2 /dev/sda2
mkswap: /dev/sda2: warning: don't erase
bootbits sectors(dos partition table detected). Use -f to
force.Setting up swap space version 1, size =
10485756 KiB LABEL=SWAP2,
UUID=986049e1-c454-484e-866c-22b38cb16e7b
Step
4: Add the swap space
to the /etc/fstab file to make it persistent at system
reboots.
[root@server
~]# vim /etc/fstab
Add the following line
LABEL=SWAP2
swap swap
defaults 0 0
Step
5: Activate the new
swap space using swapon -a.
[root@server
~]# swapon -a
Step
6: Check the swap
partition’s status using swapon -s or free -m.
[root@server
~]# swapon -s
[root@server ~]# free -m
1 comments:
super!
Post a Comment