• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / Linux: How To Add Swap on CentOS

Linux: How To Add Swap on CentOS

September 18, 2013 by Sze Hau Leave a Comment

Linux divides its physical memory (or the RAM, random access memory) into chucks of memory called pages. When a program require more memory and there isn’t enough physical memory, Linux will starts moving out inactive pages and store them on hard disk.

The process of moving out the pages for physical memory to disk is called swapping. The size of “memory on disk” is depending on the swap file size. The combination of physical memory and the “memory on disk” is called virtual memory. Therefore, you can have virtually unlimited memory.

Swapping has it disadvantage – it is slow when Linux need to keep swapping in and out of memory pages. It is slow because of the disk speed limitation. But if you have a server running with SSD disk (solid-state disk), you may not feel the pain. However, the process is required to make sure the operating system can works under memory pressure.

Check for Existing Swap File
Execute the following command to check for the existing swap space:
swapon -s

Check for Available Disk Space
Execute the following command to check for disk space availability.
df -h

Create Swap File
Execute the following command to create a swap file of size 1Gb. The following command will write zero values with block size 1024 bytes 1024k times to file “swapfile”.
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k

Enable Swap File
Execute the following command to make the file a swap space.
sudo mkswap /swapfile

Start Using Swap File
Execute the following command to let the operating system know the swap file is now ready to use.
sudo swapon /swapfile

Set Swap File Permission
Execute the following commands to ensure the swap file has the correct file permission:
chown root:root /swapfile
chmod 0600 /swapfile

Done
Execute swapon -s to ensure the swap file is being used.

Enable Swap File Durinn Boot Up
When the machine reboot, operating system will not use the swap file again. You can ensure that the swap is permanently loaded by adding it to the fstab file. Edit the file “/etc/ftab” and add the following line:
/swapfile swap swap defaults 0 0

More from my site

  • Virtualmin: How To Install Virtualmin in CentOSVirtualmin: How To Install Virtualmin in CentOS
  • MD5 and SHA1 Checksum Using LinuxMD5 and SHA1 Checksum Using Linux
  • Linux: How To Compress And Decompress Folders And FilesLinux: How To Compress And Decompress Folders And Files
  • Multiple Virtual Hosts in Apache HTTP Server (Windows)Multiple Virtual Hosts in Apache HTTP Server (Windows)
  • Java: Mortgage Payment CalculatorJava: Mortgage Payment Calculator
  • Sitestimator.com: New Badge ReleasedSitestimator.com: New Badge Released

Filed Under: Programming and Scripting, Tips and Tricks Tagged With: CentOS, Linux, swap

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in