Ensuring optimal performance is crucial when managing servers, especially those hosting resource-intensive applications like WordPress and WooCommerce. One way to enhance stability and prevent out-of-memory errors is by adding swap space. In this article, we’ll walk you through the steps to create and optimize swap space on Ubuntu, ensuring your system remains responsive even during peak loads.
Table of Contents
Why Add Swap Space?
Swap space acts as an overflow for your system’s RAM. When your physical memory is fully utilized, the system can use swap space to store inactive pages, preventing crashes and maintaining stability. However, it’s essential to use swap space judiciously, as it is significantly slower than RAM. Adding swap space to your clusters can be beneficial, but it depends on your specific use case and workload. Here are some pros and cons to consider:
Benefits of Adding Swap
- Prevents Out-of-Memory Errors:
- Swap space can act as a safety net when your system runs out of physical RAM, preventing crashes and out-of-memory errors.
- Improves Stability:
- It can improve system stability by providing additional memory resources, especially during peak loads or unexpected spikes in usage.
- Cost-Effective:
- Adding a swap is a cost-effective way to extend memory without upgrading physical RAM.
Drawbacks of Adding Swap
- Performance Impact:
- Swap space is significantly slower than physical RAM. Relying heavily on a swap can lead to performance degradation, especially for memory-intensive applications like databases and web servers.
- Increased Latency:
- Accessing data from swap space increases latency, which can affect the responsiveness of your applications.
- Disk Wear:
- Frequent swapping can lead to increased wear on SSDs, potentially reducing their lifespan.
Adding a swap can be a good idea as a fallback mechanism, but it should not be a substitute for adequate physical RAM.
Step-by-Step Guide to Adding Swap Space
Step 1: Check for Existing Swap
Before creating a new swap space, check if your system already has swap configured:
sudo swapon --show
If there’s no output, it means no swap is currently active.
Also, you can run the following command to check if the swap is deployed:
free -h
Step 2: Check Available Disk Space
Ensure you have enough disk space to create a swap file:
df -h
Step 3: Create a Swap File
Create a swap file with the desired size (e.g., 1GB – 4GB as based on our experience we don’t see any benefits of using a swap of more than 4GB. Usually 1GB-2GB is more than enough for most of the time). On our test server, we’ll deploy a 2GB swap file using the following commands:
sudo fallocate -l 2G /swapfile
Note! If fallocate
is not available, use dd
:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
Verify if the correct amount of space was reserved:
ls -lh /swapfile
Step 4: Secure the Swap File
Set the correct permissions to secure the swap file, and make it only accessible to the root user:
sudo chmod 600 /swapfile
Step 5: Mark the File as Swap Space
Set up the swap file:
sudo mkswap /swapfile
Step 6: Enable the Swap File
Activate the swap file:
sudo swapon /swapfile
Step 7: Make the Swap File Permanent
Add the swap file to /etc/fstab
to make it permanent:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Optimizing Swap Usage and Tuning its Settings
To ensure swap space is used efficiently and only when necessary, adjust the following settings: vm.swappiness
and vm.vfs_cache_pressure
.
Please note! Adjusting the values of vm.swappiness
and vm.vfs_cache_pressure
can be important, but you have to understand the implications of doing so.
Lowering vm.swappiness
means the kernel will prefer to keep data in RAM rather than swapping it out to disk, which can reduce latency and improve performance for applications requiring quick data access, such as databases and web servers.
However, this also increases the risk of out-of-memory (OOM) errors and may lead to inefficient memory usage if the system doesn’t utilize available swap space effectively.
Similarly, lowering vm.vfs_cache_pressure
helps retain directory and inode caches longer, enhancing file system performance and reducing disk I/O operations. Yet, this too can lead to OOM conditions if the system runs out of RAM and may result in potential memory waste if too much memory is allocated to caching.
To strike a balance between performance and stability, monitor your system’s performance using tools like vmstat
, free,
and top
, and make gradual adjustments to these values.
Ensure your system has enough RAM to handle your workload without relying heavily on swap space. In summary, while lowering vm.swappiness
and vm.vfs_cache_pressure
can boost performance by keeping more data in RAM, and it also heightens the risk of OOM errors. Carefully monitor and adjust these settings to find the optimal configuration for your specific usage and case.
Generally, setting vm.swappiness
between 1 and 5 ensures the system avoids using swap space as much as possible, only resorting to it when absolutely necessary. This can enhance performance but also increase the risk of out-of-memory errors if your RAM is fully utilized.
For vm.vfs_cache_pressure
, a recommended range is 10 to 50. Setting it to 10 means the system will retain directory and inode caches longer, boosting file system performance. However, setting it too low can lead to inefficient memory usage if too much memory is allocated to caching.
To achieve the best results, start with conservative values like vm.swappiness=3
and vm.vfs_cache_pressure=30
, and gradually adjust them while monitoring system performance and memory usage. Striking a balance between performance and stability is key, ensuring your system has enough RAM to handle your workload without heavily relying on swap space.
Adjust Swappiness
The swappiness
value controls how often the system uses swap. A lower value means the system will use swap less frequently:
sudo sysctl vm.swappiness=3
To make this change permanent, add it to /etc/sysctl.conf
:
echo 'vm.swappiness=3' | sudo tee -a /etc/sysctl.conf
Adjust Cache Pressure
The vfs_cache_pressure
value controls the tendency of the kernel to reclaim memory used for caching directory and inode objects:
sudo sysctl vm.vfs_cache_pressure=30
To make this change permanent, add it to /etc/sysctl.conf
:
echo 'vm.vfs_cache_pressure=30' | sudo tee -a /etc/sysctl.conf
Verify Swap Configuration
Finally, check the swap configuration to ensure everything is set up correctly by running these two commands:
sudo swapon --show
free -h
Adding and optimizing swap space on your Ubuntu server can significantly enhance stability and prevent out-of-memory errors, especially during peak loads. By following the steps outlined in this guide, you can ensure your system remains responsive and performs optimally.
Note! Please remember, that while swap space is a valuable tool, it should not replace adequate physical RAM. Monitor your system’s performance and adjust configurations as needed to maintain the best possible performance.
Feel free to ask the Blorax Team, share your experiences, or ask any questions in the comments below!
Leave a Reply