DEV Community

AJAY SHRESTHA
AJAY SHRESTHA

Posted on

2

Increase Swap Memory in Ubuntu

Swap memory serves as a backup for your system's RAM, utilizing the hard disk to provide additional space when your RAM is fully utilized. While it is slower than RAM due to the hard disk's lower speed, it is crucial in preventing system crashes by allowing the operating system to offload less frequently used data from memory.
Increasing your swap space can be a simple yet effective solution if you're experiencing slow performance or need to run applications requiring significant memory resources.

Step-by-Step Guide to Increasing Swap Memory

Step 1: Check Current Swap Space
Before making any changes, check your current swap space using:

sudo swapon --show
Enter fullscreen mode Exit fullscreen mode

This command lists all active swap partitions and files. If there's no output, you don't have any swap space configured.

Step 2: Check Available Disk Space
Ensure you have enough free disk space by running:

df -h
Enter fullscreen mode Exit fullscreen mode

This command shows your disk space in a human-readable format.

Step 3: Create a Swap File
Create a swap file with:

sudo fallocate -l 2G /swapfile
Enter fullscreen mode Exit fullscreen mode

This allocates 2 gigabytes for the swap file. Adjust the size as needed.

Step 4: Enable the Swap File
Set the correct permissions:

sudo chmod 600 /swapfile
Enter fullscreen mode Exit fullscreen mode

Format the swap file:

sudo mkswap /swapfile
Enter fullscreen mode Exit fullscreen mode

Enable the swap file immediately:

sudo swapon /swapfile
Enter fullscreen mode Exit fullscreen mode

Verify it's active:

sudo swapon --show
Enter fullscreen mode Exit fullscreen mode

Step 5: Make the Swap File Permanent
Backup your fstab file:

sudo cp /etc/fstab /etc/fstab.bak
Enter fullscreen mode Exit fullscreen mode

Add the swap file to fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Enter fullscreen mode Exit fullscreen mode

Step 6: Adjust Swappiness and Cache Pressure

Swappiness controls how often your system uses swap space. A lower value (e.g., 10) means the system prefers RAM over swap, while a higher value (e.g., 100) increases swap usage.

Adjust how often the system uses swap space:

sudo sysctl vm.swappiness=10
Enter fullscreen mode Exit fullscreen mode

Cache pressure affects how the system manages cached files. A lower value (e.g., 50) reduces the frequency of cache regeneration.

Improve cache performance:

sudo sysctl vm.vfs_cache_pressure=50
Enter fullscreen mode Exit fullscreen mode

To make Swappiness and Pressure permanent, add lines below at the bottom of /etc/sysctl.conf:

sudo nano /etc/sysctl.conf

# Add lines given below at bottom of /etc/sysctl.conf
vm.swappiness=10
vm.vfs_cache_pressure=50
Enter fullscreen mode Exit fullscreen mode

Save and close the file.

Step 7: Removing Old Swap Files

sudo swapoff /path/to/old/swapfile
Enter fullscreen mode Exit fullscreen mode

Remove the entry from fstab and delete the file.

Conclusion
Increasing swap memory can significantly improve your system's performance, especially when running memory-intensive applications. Following these steps ensures your system has the resources it needs to run smoothly. Remember to monitor your memory usage and adjust settings as required for optimal performance.

πŸ‘‹ While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay