Swap is virtual memory in Linux that expands the physical memory to store temporary data. It is similar to virtual memory in Windows. In Windows, only files can be used as virtual memory. Linux can be used as a file or partition as a virtual memory.
First look at the current memory and swap space size (the default unit is K, the M Unit is m.):
Free-m
Here you can see that the total memory is 503m,swap does not exist.
View swap information, including details of files and partitions
# swapon-s
Or
# Cat/proc/swaps
If not, we will need to manually add the swap partition. Note that the OPENVZ architecture of the VPS is not supported by manually adding swap partitions.
There are two options for adding swap space: Adding an interchange partition or adding a swap file. It is recommended that you add an interchange partition, but if you do not have much free space available, add the swap file.
I. Adding swap files
1, using the DD command to create a swap file
DD If=/dev/zero Of=/home/swap bs=1024 count=1024000
This creates a/home/swap partition file with a size of 1G.
2. Production as swap format file:
Mkswap/home/swap
3, and then use the Swapon command to put this file partition to mount the swap partition
/sbin/swapon/home/swap
We use the FREE-M command to see that there are already swap partitions.
But after restarting the system, the swap partition becomes 0 again.
4, to prevent the restart after the swap partition becomes 0, to modify the/etc/fstab file
Vi/etc/fstab
At the end of the file (last line), add
/home/swap Swap default 0 0
So even if you reboot the system, the swap partition still has a value.
Delete swap file
1. Stop the Swap section first
/sbin/swapoff/home/swap
2. Delete the swap section file
Rm-rf/home/swap
3, delete the automatic mount configuration command
Vi/etc/fstab
This line deletes
/home/swap Swap default 0 0
This allows you to delete the manually added swap file.
Attention:
1. Adding delete swap can only be done using the root user.
2, the allocation of the system when the swap partition can not seem to delete.
3, swap partition is generally twice times the memory, but the maximum is not more than 2G
Second, use the partition to do swap (virtual memory).
1 Use Fdisk to create the swap partition (assuming/DEV/SDB2 is the swap partition you created)
2 Use the Mkswap command to set up the swap partition:
# MKSWAP/DEV/SDB2
3 Enable the Swap partition:
# SWAPON/DEV/SDB2
4 write to/etc/fstab to enable at boot time:
/DEV/SDB2 Swap Defaults 0 0
Remove Swap partition
The steps are as follows:
1. Stop the Swap section first
/sbin/swapoff/dev/sdb2
2, delete the automatic mount configuration command
Vi/etc/fstab
This line deletes
/DEV/SDB2 Swap Defaults 0 0
This will remove the manually added swap partition.