Creating Swap (Virtual Memory) and RAM Optimization on Linux Servers
When databases (MySQL/MariaDB) or game add-ons running on Linux servers momentarily consume excessive RAM, the operating system activates the protection mechanism and turns off the main services (OOM Killer) to prevent the server from crashing. To prevent this situation, there is a device on the disk that works like RAM. Swap (Virtual Memory) field must be created. Here is the Swap configuration step by step.
Step 1: Checking Current Swap Status
To check if your server already has virtual memory, type the following command:
swapon --showorfree -m- If the Swap line appears empty or "0", immediately go to step 2 and create a new field.
Step 2: Creating Swap File (Ex: 4 GB)
We will allocate 4 Gigabytes of space from your system disk as virtual RAM:
- Create the empty file:
fallocate -l 4G /swapfile - Edit the security permissions of the file so that it can only be opened to the root user (This is a critical security step):
chmod 600 /swapfile - Convert the file to swap format:
mkswap /swapfile - Activate the created virtual memory:
swapon /swapfile
Step 3: Making the Swap Area Permanent and Swappiness Setting
To prevent virtual memory from being lost when the server is rebooted, we must add it to the fstab file:
/etc/fstabOpen the file with nano or vi and add the following code at the bottom line:/swapfile none swap sw defaults 0 0- Swappiness Optimization: It determines whether the operating system will start using Swap space when the physical RAM decreases to what percentage. You can set this value to avoid tiring the server performance.
10or20It is ideal to do:sysctl vm.swappiness=10
This article is specially prepared for PvPServer.