Enhance Linux Performance: Step-by-Step Guide to Increase Swap File Space

iamakshaykaushik

Akshay Kaushik

Posted on January 5, 2024

Enhance Linux Performance: Step-by-Step Guide to Increase Swap File Space

In Linux, swap space allows the system to utilize disk storage for memory management. When the system is running low on physical RAM, it can temporarily move data from memory to swap space on the hard drive.

Increasing swap file size provides more “breathing room” for memory and can improve system performance when running memory-intensive applications.

This article explains what swap space is, why you may need to increase it, and a step-by-step process to resize the Linux swap file using the command line.

What is swap space?

Think of swap space as a temporary storage area that your computer uses when it's running out of physical memory (RAM). It's like a lifeboat for your system when it's drowning in heavy applications.

When to increase swap space

There are a few common cases when increasing Linux swap space can improve performance:

  • Memory intensive workloads: Applications like data analysis with Pandas, machine learning with NumPy and SciPy, and product analytics with Apache Spark require significant memory. Increasing swap space prevents slow downs when working with large datasets.

  • Multitasking with many applications: Running multiple applications causes increased memory utilization. More swap space ensures smooth concurrent execution.

  • Susceptibility to memory exhaustion: Some Linux servers are vulnerable to denial-of-service attacks using up memory. Added swap space gives breathing room to recover safely.

  • Prevent out-of-memory kernel panics: In extreme cases, Linux can completely run out of memory and kernel panic. Extra swap prevents this server crash by allowing graceful degradation.

Check your current swap space utilization to determine if your system needs more swap area on disk.

Check current swap usage

swapon --show
Enter fullscreen mode Exit fullscreen mode

This command displays the Linux swap partitions and files currently in use along with their size in kilobytes. It also shows the total swap memory utilization across all swap areas on disk.

If swap usage is close to 100% or you’re getting out-of-memory errors, increase the swap size.

Disable the swap file

sudo swapoff /name.swap
Enter fullscreen mode Exit fullscreen mode

Before resizing an existing swap file, it needs to be temporarily disabled with the swapoff command.

This stops the OS from writing new swap data so the file can be safely manipulated.

Create a new larger swap file

sudo dd if=/dev/zero of=/name.swap.tmp bs=1024 count=2097152
Enter fullscreen mode Exit fullscreen mode

Use the dd command to quickly create a new 2GB file to use as larger swap space.

We give this file a temporary name like name.swap.tmp to avoid overwriting the old swap file before the changeover is complete.

Rename and replace the old swap file

sudo mv /name.swap /name.swap.old
sudo mv /name.swap.tmp /name.swap
Enter fullscreen mode Exit fullscreen mode

Now that the new swap file is created, rename the original swap file to a backup name like name.swap.old.

Then rename the new name.swap.tmp file to the original swap file name name.swap.

Set permissions on the new swap file

sudo chmod 600 /name.swap
Enter fullscreen mode Exit fullscreen mode

The new swap file needs its permissions changed to allow only root read/write access for security reasons. The chmod command does this quickly.

Re-enable the new swap file

sudo mkswap /name.swap
sudo swapon /name.swap
Enter fullscreen mode Exit fullscreen mode

Initialize the new swap file using mkswap, then enable it for use with swapon.

The system can now start utilizing the new, larger swap space.

Verify the new swap file size

swapon --show
free -h
Enter fullscreen mode Exit fullscreen mode

Check swapon and free commands to confirm your newly resized swap file is reported correctly.

You should see the increased swap size along with lower utilization percentage.

Reboot the system

sudo reboot
Enter fullscreen mode Exit fullscreen mode

After completing the swap file resizing steps, safely reboot Linux to allow services and applications use the added memory.

FAQs

1. Why is increasing swap file size beneficial?

- It provides extra 'virtual' memory, which helps your system handle more applications simultaneously.

2. Can I increase my swap file size without rebooting the server?

- No, you need to reboot the server for changes to take effect.

3. What if my swap space is not showing any increase after following these steps?

- Try disabling and re-enabling the swap space again.

4. Is there any limit to how much I can increase my swap file size?

- Yes, it is recommended that your swap space does not exceed 2x the size of your physical RAM.

5. Can I decrease my swap file size in the same way?

- Yes, follow these same steps but specify a lower value when resizing the swap file.

I require immediate work. Entry-level is also acceptable.

💖 💪 🙅 🚩
iamakshaykaushik
Akshay Kaushik

Posted on January 5, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

PostgreSQL Full Text Search Rank by Position
postgressql PostgreSQL Full Text Search Rank by Position

November 30, 2024