This guide from EcoStack Cloud explains how to add swap space to your Debian 12 system. Adding swap can improve system performance and stability, especially when handling memory-intensive applications on your NVMe VPS.
debianswap
XXXLarge
VPS
8
vCPU
32 GB
Memory
320 GB
NVMe Disk
16384 GB
Traffic
76.90€
/month
* Up to 16 vCPU, 96GB RAM and 960GB NVMe Disk Space
#### Prerequisites
Before you start, make sure you have:
- A Debian 12 server from [EcoStack Cloud](https://ecostack.cloud).
- Sudo access to the server.
#### Step 1: Check Existing Swap Space
First, check if your system already has swap space and its size.
```bash
sudo swapon --show
```
You can also verify this by checking the `/proc/swaps` file or using the `free` command:
```bash
cat /proc/swaps
free -h
```
If there is no existing swap, these commands will show no output for swap or display it as zero.
#### Step 2: Create a Swap File
Create a swap file using the `dd` command. Replace `2G` with the desired swap file size.
```bash
sudo dd if=/dev/zero of=/swapfile bs=1G count=2
```
This command creates a 2GB swap file. You can adjust the size by changing the `count` value (e.g., `count=4` for 4GB).
#### Step 3: Secure the Swap File
Set the correct permissions for the swap file to ensure that only root can read and write to it.
```bash
sudo chmod 600 /swapfile
```
#### Step 4: Set Up the Swap Space
Format the file as swap space using the `mkswap` command.
```bash
sudo mkswap /swapfile
```
Activate the swap file to enable it immediately.
```bash
sudo swapon /swapfile
```
Verify that the swap is active by checking again:
```bash
sudo swapon --show
```
You should now see your new swap file listed.
#### Step 5: Make the Swap File Permanent
To ensure that the swap file is used automatically at boot, add it to the `/etc/fstab` file.
```bash
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
```
This line tells the system to use the swap file at boot.
#### Step 6: Adjust Swappiness
Swappiness determines how often the system uses swap. The default value is 60, but you can adjust it to optimize performance. Lower values make the system less likely to use swap.
To check the current swappiness:
```bash
cat /proc/sys/vm/swappiness
```
To set a new value, for example, 10:
```bash
sudo sysctl vm.swappiness=10
```
To make this change permanent, add it to the `/etc/sysctl.conf` file:
```bash
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
```
#### Step 7: Adjust Cache Pressure
You can also adjust the `vfs_cache_pressure` setting, which controls the tendency of the kernel to reclaim the memory used for caching. Lower values make the kernel retain more file system cache.
To check the current value:
```bash
cat /proc/sys/vm/vfs_cache_pressure
```
To set a new value, for example, 50:
```bash
sudo sysctl vm.vfs_cache_pressure=50
```
To make this change permanent, add it to the `/etc/sysctl.conf` file:
```bash
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
```
#### Conclusion
You have successfully added and configured swap space on your Debian 12 server provided by [EcoStack Cloud](https://ecostack.cloud). Adding swap space can significantly enhance your system's ability to handle memory-intensive tasks and improve overall performance.
For further reading and advanced configurations, refer to the [official Debian documentation](https://wiki.debian.org/Swap).
---