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

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 swapspace?

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 swapspace

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 swapusage

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 swapfile

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 swapfile

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 swapfile

sudo mv /name.swap /name.swap.oldsudo 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.

Recommended by LinkedIn

Docker : Linux Container (LXC) Saurav Kumar 8 years ago
Mastering File Compression in Linux: A Guide to zip… Rafsan Anwar 1 week ago
Exploring the Depths of Linux File Systems - Part 3:… Mohammad Parvez 2 months ago

Set permissions on the new swapfile

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 swapfile

sudo mkswap /name.swapsudo 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 filesize

swapon --showfree -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 thesystem

sudo reboot 

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

FAQs

Q1. Why is increasing swap file size beneficial?

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

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

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

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

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

Q4. 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.

Q5. 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.

Enhance Linux Performance: Step-by-Step Guide to Increase Swap File Space (2024)
Top Articles
How to Repair Your iPhone Charger
Ironman Guide/Herblore
Scheelzien, volwassenen - Alrijne Ziekenhuis
Oldgamesshelf
Skylar Vox Bra Size
Www.fresno.courts.ca.gov
FFXIV Immortal Flames Hunting Log Guide
Kansas Craigslist Free Stuff
Chris wragge hi-res stock photography and images - Alamy
Alpha Kenny Buddy - Songs, Events and Music Stats | Viberate.com
Nieuwe en jong gebruikte campers
Celsius Energy Drink Wo Kaufen
Wordscape 5832
General Info for Parents
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
Bx11
Carolina Aguilar Facebook
Nhl Tankathon Mock Draft
Ratchet & Clank Future: Tools of Destruction
Mccain Agportal
Yard Goats Score
Bible Gateway passage: Revelation 3 - New Living Translation
O'Reilly Auto Parts - Mathis, TX - Nextdoor
Cincinnati Adult Search
How Long After Dayquil Can I Take Benadryl
Mini Handy 2024: Die besten Mini Smartphones | Purdroid.de
2487872771
Sherburne Refuge Bulldogs
Bay Area Craigslist Cars For Sale By Owner
Kirk Franklin Mother Debra Jones Age
Parent Management Training (PMT) Worksheet | HappierTHERAPY
Ff14 Sage Stat Priority
2430 Research Parkway
Ellafeet.official
Att U Verse Outage Map
Daily Journal Obituary Kankakee
Orangetheory Northville Michigan
T&J Agnes Theaters
Scottsboro Daily Sentinel Obituaries
Thanksgiving Point Luminaria Promo Code
Saybyebugs At Walmart
Timberwolves Point Guard History
Lake Kingdom Moon 31
Tgirls Philly
Citibank Branch Locations In North Carolina
Elven Steel Ore Sun Haven
Rescare Training Online
Oefenpakket & Hoorcolleges Diagnostiek | WorldSupporter
Who uses the Fandom Wiki anymore?
116 Cubic Inches To Cc
Ewwwww Gif
Sunset On November 5 2023
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 5924

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.