How to Increase swap memory size in Linux

You must have faced a condition when you need to increase your swap size. Well I faced one when I was trying to install Oracle Database and I was out of swap memory.

I will show you two ways which can be used to do the same.

Method 1

If you have additional space in your hard disk create swap based partition.

# fdisk /dev/sda  
Command (m for help): n  
 Command action  
    e   extended  
    p   primary partition (1-4)  
 e  
 Partition number (1-4): 1  
 First cylinder (1-48699, default 1): 1  
 Last cylinder, +cylinders or +size{K,M,G} (1-48699, default 48699): +100M  
 Command (m for help): p

Disk /dev/sda: 51.1 GB, 51064602624 bytes  
 64 heads, 32 sectors/track, 48699 cylinders  
 Units = cylinders of 2048 * 512 = 1048576 bytes  
 Sector size (logical/physical): 512 bytes / 512 bytes  
 I/O size (minimum/optimal): 512 bytes / 512 bytes  
 Disk identifier: 0x3a369b23  
 Device    Boot      Start         End      Blocks   Id  System  
/dev/sda4                    1         101      103408    5   Extended  
 Command (m for help): t  
 Selected partition 1  
 Hex code (type L to list codes): L  
 Hex code (type L to list codes): 82  
 Command (m for help):w  

Once the partition is created run the below commands

# partprobe /dev/sda4  
# mkswap /dev/sda4  

To utilize the created partition as swap and making it "ON"

# swapon /dev/sda4  

To check the swap report

# swapon -s   

Ensure that when the system reboots, the swap file is made available to the kernel

# vi /etc/fstab  
/dev/sda4                    swap                   swap     defaults        0 0  

Update the fstab file

# mount -a  

If you want to turnoff the swap partition

# swapoff  

To check the swap partitions

# swapon -s  

Check the swap memory

# free -m

Method 2
If you do not have additional space in your hard disk create swap based file.

Create 512MB swap file

dd if=/dev/zero of=/swapfile bs=1024 count=524288  

Calculation:

For 512 MB = 512 * 1024  
For 2 GB = (2*1024) * 1024  
# mkswap /swapfile   
# swapon /swapfile  

Ensure that when the system reboots, the swap file is made available to the kernel

# vi /etc/fstab  
/swapfile                    swap                   swap     defaults        0 0  

Update the fstab file

# mount -a

Related Articles

What is swappiness and how do we change its value?
What is kernel-PAE in Linux?

What is a Kernel in Linux?
What is virtual memory, paging and swap space?