Setting Up Virtual Swap Memory

1 min read

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.

Setting Up Virtual Swap Memory

Check Server Memory

free -h

Current server memory:c8fbbd72-c73e-4724-9ec7-7ca5c5135efe.jpg

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

1f6cf7a3-9bd8-4a5e-a665-821643af6801.jpg

Set Up the swap Partition File

mkswap /opt/swap

Enable the Swap Partition File

swapon /opt/swap

Check it now

free -h

56c8953f-fe6f-42f7-b05f-6cf5d107e40c.jpg

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