Add Virtual Memory#
-
Get file blocks (The number of swap partitions depends on your physical memory and disk usage, usually 1-2 times the size of physical memory)
dd if=/dev/zero of=/var/swapfile bs=1M count=2048 # The block_size and number_of_block can be customized, for example, bs=1M count=1024 represents setting a 1GB swap partition.
-
Create swap file
/sbin/mkswap -f /var/swapfile
-
Give 600 permission to swapfile
chmod 600 /var/swapfile
-
Activate swap file
/sbin/swapon /var/swapfile
-
Check if swap is correct
/sbin/swapon -s
-
Add to fstab file for automatic startup during system boot (to prevent swap partition from becoming 0 after restart)
vi /etc/fstab // Add at the end /var/swapfile swap swap defaults 0 0
-
Reboot the system
reboot
-
Check the status of swap partitions
free -m
Remove Virtual Memory#
- Stop the swap partition
/sbin/swapoff /var/swapfile
- Delete the swap partition file
rm -rf /var/swapfile
- Delete the automatic mount configuration command
vi /etc/fstab // Delete the following content /var/swapfile swap swap defaults 0 0