Linux systems implement virtual memory in two ways: swap (swap partition) and exchange files
Interchange File
- View Memory:
free -m
, -m is displayed in units of MB, -g unit GB - Create a file:
touch /root/swapfile
- Use
dd
the command to create a file size of the swapfile 2G:
dd if=/dev/zero of=/root/swapfile bs=1M count=2048
// End command waiting period
if the input file indicates input_file
of output file represents output_file
bs block_size represents the block size
count represents a count.
Here, I used a block size of 1M, the number of data blocks 2048, space is allocated in this way the size of 2G. - Interchange File Format:
mkswap /root/swapfile
- Enable swap file:
swapon /root/swapfile
- Boot automatically load virtual memory, add the following command in the / etc / fstab file:
/root/swapfile swap swap defaults 0 0
- After the restart to take effect
reboot
If you want to delete the swap partition and the swap file, against the above order of operations:
- Delete the swap file / etc / fstab file, add the line
- Disable swap file
swapoff /root/swapfile
- Delete the swap file
rm -fr /root/swapfile
Swap partition
The use of swap approach is actually to create a new partition, and then mount the partition as swap space, with the traditional method steps as the new partition. Just format the partition and mount partitions were used mkswap and swapon command. Before creating a partition, we often used fdisk -l and df -Th command to view disk information and mount information to determine the size of the partition.
- Hdb1 create partitions in a shell as root
fdisk /dev/hdb1
and then save and exit w - Set swap partition, as the case may set up their own size.
mkswap /dev/hdb1
- Enabling swap
swapon /dev/hdb1
- Boot automatically load virtual memory, add the following command in the / etc / fstab file:
/dev/hdb1 swap swap defaults 0 0