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
Bad Real Estate Photos Posted to Have Fun
How to Travel On a Budget in Costa Rica
#ridwork guides | fountainpenguin
Kevin Cox Picks
Washu Parking
13 Easy Ways to Get Level 99 in Every Skill on RuneScape (F2P)
Shs Games 1V1 Lol
Gabrielle Abbate Obituary
Holly Ranch Aussie Farm
Tyrunt
Hay day: Top 6 tips, tricks, and cheats to save cash and grow your farm fast!
ds. J.C. van Trigt - Lukas 23:42-43 - Preekaantekeningen
Bill Devane Obituary
Milk And Mocha GIFs | GIFDB.com
Otr Cross Reference
What Was D-Day Weegy
Purple Crip Strain Leafly
Flower Mound Clavicle Trauma
Dc Gas Login
Truth Of God Schedule 2023
Craigslist In Flagstaff
使用 RHEL 8 时的注意事项 | Red Hat Product Documentation
Lonesome Valley Barber
Persona 4 Golden Taotie Fusion Calculator
Milanka Kudel Telegram
Is Windbound Multiplayer
Project Reeducation Gamcore
Jeff Nippard Push Pull Program Pdf
Bocca Richboro
Koninklijk Theater Tuschinski
Kirsten Hatfield Crime Junkie
Spectrum Outage in Queens, New York
Encore Atlanta Cheer Competition
Anesthesia Simstat Answers
Hwy 57 Nursery Michie Tn
Alternatieven - Acteamo - WebCatalog
Craftsman Yt3000 Oil Capacity
What Is Opm1 Treas 310 Deposit
James Ingram | Biography, Songs, Hits, & Cause of Death
Here’s how you can get a foot detox at home!
Pitchfork's Top 200 of the 2010s: 50-1 (clips)
Weapons Storehouse Nyt Crossword
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Former Employees
Courtney Roberson Rob Dyrdek
Craigslist Houses For Rent Little River Sc
Wolf Of Wallstreet 123 Movies
Keci News
Vci Classified Paducah
Neil Young - Sugar Mountain (2008) - MusicMeter.nl
Wwba Baseball
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.