DEV Community

Vuyisile Ndlovu
Vuyisile Ndlovu

Posted on • Originally published at vuyisile.com on

How to increase swap space in Linux

I recently had to run a RAM intensive task on a system with limited RAM. To make the task run successfully, I created a swap file large enough to hold the application’s data. In this post, I’ll show you how to increase the swap space on your computer.

Swap memory or swap space is an extension of the computer’s RAM created on the hard drive. It is useful in preventing crashes in running programs when the available system RAM is exhausted. It isn’t a substitute for real RAM but having it helps give your programs a little more wiggle room. To add more swap space, do the following:

Create a swap file


sudo dd if=/dev/zero of=/swapfile_extend_60GB bs=1GB count=60

Enter fullscreen mode Exit fullscreen mode

if=/dev/zero is the input file. /dev/zero is a special input file that returns as many null characters as a read operation requests. This is useful for creating a contiguous file without any holes in it, which is exactly what we need to do for swap memory.

Next, we specify of=/swapfile_extend_60 which is the output file. bs is the block size. Here we create 60 blocks of 1GB each, making a 60GB file.

Set Permissions

We need to set permissions to only allow root to interact with this file:


sudo chmod 600 /swapfile_extend_60GB

Enter fullscreen mode Exit fullscreen mode

Set Up Swap area

Next, initialise the file you just created as a swap file by running mkswap on it:


sudo mkswap /swapfile_extend_60GB

Enter fullscreen mode Exit fullscreen mode

Enable The Swap File

Activate and make the swap file available to the system by running:


sudo swapon /swapfile_extend_60GB

Enter fullscreen mode Exit fullscreen mode

Verify Swap Creation


# Verify the swap was created
free -h
# OR
sudo swapon --show

Enter fullscreen mode Exit fullscreen mode

Make The Change Permanent

To make the change permanent, add an entry in /etc/fstab. The /etc/fstab file is a system configuration file that defines how disks and files should be mounted:


sudo nano /etc/fstab

# Add the following line
/swapfile_extend_60GB none swap sw 0 0

Enter fullscreen mode Exit fullscreen mode

Save the file and close it.

Disable The Swap File

To disable the swap file run swapoff on it and delete it to recover disk space:


sudo swapoff /swapfile_extend_60G

sudo rm /swapfile_extend_60G

Enter fullscreen mode Exit fullscreen mode

Conclusion

You’ve seen how to create and enable a swap file in a Linux operating system.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay