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

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

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

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

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

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

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

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

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

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


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 need immediate work. Entry-level is also acceptable.