Setting Up Virtual Swap Memory
I bought a 2-core 4GB server on Alibaba Cloud to set up a Don't Starve game server, found the memory was insufficient, and solved it with swap memory.

Check Server Memory
free -h
Current server memory:
Create the Partition File
(bs is the size of each block, count is how many blocks the file has; here the swap is 4G)
dd if=/dev/zeroof=/opt/swap bs=1M count=4096

Set Up the swap Partition File
mkswap /opt/swap
Enable the Swap Partition File
swapon /opt/swap
Check it now
free -h

Swap now has 4G of space
As for virtual memory, the best setting is 1 to 2 times your physical memory; choose according to your needs. That’s about it — this should solve some CentOS out-of-memory problems.
How to Remove the Virtual Memory
View swap
cat /proc/swaps
Stop the swap partition
shell
Then delete the swap file
rm -rf /opt/swap
Comments