Swap space in Linux is used when physical memory RAM is full. If the system requires more memory resources and the physical memory is full, the inactive pages in the memory will be moved to the Swap space.
Use parted or fdisk to create Swap space. Parted is easier to use than fdisk. Therefore, only parted is described here. To use parted to create Swap space:
Run the command parted/dev/hdb as the root user at the shell prompt. Here/dev/hdb is the name of your device with a free space hard drive.
At the (parted) prompt, type print to view the number of existing partitions and free space. The start and end values are in MB. Determine the number of free space on the hard drive and the amount of space you want to allocate to the newly created Swap space.
At the (parted) prompt, type mkpartfs part-type linux-swap start end. Here, part-type is one of primary, extended, and logical, and start is the starting point of Swap partition, end is the termination point of the partition.
Warning changes occur immediately. Exercise caution when typing.
Type quit to exit parted.
Now you can create a Swap space and use the mkswap command to set the Swap space. In the shell prompt, enter the following command as the root user:
Mkswap/dev/hdb2
To enable swap partition immediately, type the following command:
Swapon/dev/hdb2
To enable it during boot, edit the/etc/fstab file to include the following lines:
/Dev/hdb2 swap defaults 0 0
The new Swap space is Enabled Next time the system boots.
After Swap space is added and enabled, check the output of cat/proc/swaps or the free command to ensure that the Swap partition is enabled.
To add a swap file: determine the size of the new swap file. Multiply the size by 1024 to determine the size of the block. For example, the block size of a 64 MB swap file is 65536.
In the shell prompt, enter the following command as the root user, where count is equal to the expected block size:
Dd if =/dev/zero of =/swapfile bs = 1024 count = 65536
Run the following command to set the file exchange:
Mkswap/swapfile
To enable the swap file immediately instead of automatically enabling it during boot, run the following command:
Swapon/swapfile
To enable it during boot, edit the/etc/fstab file to include the following lines:
/Swapfile swap defaults 0 0
The new swap file is Enabled Next time the system boots.
After Swap space is added and enabled, check the output of cat/proc/swaps or free command to ensure that the Swap partition has been started.