DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How to Change the Size of Your Swap File

Whether your box has run out of space or you have a server with a lot of processes running on it, managing swap space is an essential aspect of system administration.

Babak Ghazvehi user avatar by
Babak Ghazvehi
·
Feb. 10, 17 · Tutorial
Like (0)
Save
Tweet
Share
7.89K Views

Join the DZone community and get the full member experience.

Join For Free

One of the most frequently asked questions in support revolves around how to change the size of the swap file. I'll tackle this topic by providing a short introduction to the matter, with a couple of use cases and a step-by-step guide. Let's get started!

What Is It?

Swap (Virtual Memory) is space on a disk that Linux can use it when it runs out of RAM. Inactive processes can be moved to swap from RAM to make space for active processes.

However, there is a disadvantage since hard disk is much slower than RAM, your server performance can slow down.

Cloud 66 has created a swap file on provisioning servers in root folder /c66-swapfile. The size of the file depends on the free space of the server that is between 3 GB to 256 GB.

Reasons to Change the Size of the Swap File

You may need to change swap in different cases. For example:

  • When your box runs out of spaces, decreasing swap file size is the quickest way that you can release some spaces, especially when you have a huge swap file.
  • When you have a server with lots of processes running on it, increasing swap file size will let the kernel to move inactive processes into swap to make room for active processes in the working memory.

Step-by-Step Guide

The instructions below will help you change your server swap file size to the desired value.

First, you need to create a script that you can re-use.

Create a file named /tmp/swap-file-setter.sh with the script below:

#!/bin/bash 
# don't do anything if we already have swap file settings 
if [[ $(swapon -s | wc -l) = 1 ]] ; then 
  echo "Creating your swap file" 
  dd if=/dev/zero of=/c66-swapfile bs=1024 count=$1k 
  mkswap /c66-swapfile swapon /c66-swapfile 
  echo "/c66-swapfile none swap sw 0 0 " >> /etc/fstab 
  chown root:root /c66-swapfile 
  chmod 0600 /c66-swapfile 
  echo "Swap file successfully created" 
fi 

Give executing permission to the script:

sudo chmod +x /tmp/swap-file-setter.sh 

Now, you can use the script each time you wish to change the swap file size.

Remove the current swap file.

$ sudo swapoff -a $ sudo rm -f /c66-swapfile 

Create a new swap file using the script above.

sudo /tmp/swap-file-setter.sh <size_in_mb> 

To sum up, managing swap space is an essential aspect of system administration. Always monitor your memory to ensure that you are getting the best result with your current swap file size.

Published at DZone with permission of Babak Ghazvehi. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Real-Time Supply Chain Control Tower Powered by Kafka
  • Java Development Trends 2023
  • Kotlin Is More Fun Than Java And This Is a Big Deal
  • How to Develop a Portrait Retouching Function

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: