Sometimes the default physical memory is not enough to serve certain tasks or processes. It may happen that the processes are swapping more memory in and out of the swap space. This post helps to explain how to increase the swap memory on a Linux system.
There will be 3 different ways explained in this post, it depends on the availability of each case to use any of these options.
Method #1 : Use a new disk
1. Add a new disk to the Linux instance from the available storage.
2. Once the new disk is presented to the instance and the OS has detected it, run the below commands to create the new swap space/partition on this new disk. Be aware that the remaining space on the disk can be used for any other file system as required.
$ sudo gdisk /dev/sdb GPT fdisk (gdisk) version 0.8.10 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): n Partition number (1-128, default 1): First sector (34-104857566, default = 2048) or {+-}size{KMGTP}: Last sector (2048-104857566, default = 104857566) or {+-}size{KMGTP}: 4096000 ### Select the desired size Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): 8200 ### Type L to list all the FS types an select the Linux Swap. Changed type of partition to 'Linux swap' Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y OK; writing new GUID partition table (GPT) to /dev/sdb. The operation has completed successfully.
3. Run the following command to reload the changes on the partition table.
$ sudo partprobe /dev/sdb
4. Create a new swap area on the new partition that had been just created:
$ sudo mkswap /dev/sdb1 Setting up swapspace version 1, size = 1020 KiB no label, UUID=eeb4e9e0-386f-4fbd-919a-130e2c17079e
5. Add the swap entry to the /etc/fstab file as indicated below on this step, the best recommendation is to use UUID of the partition instead the name.
UUID=1a50efd3-ddc6-4dab-b3ad-xxxxxxxx swap swap defaults,_netdev,x-initrd.mount 0 0
6. Load the new swap space that had been created for the Instance to start using it when needed, the below command needs to be issued.
# sudo swapon -a
7. To list the swap devices run the below command.
# sudo swapon -s
8. Finally, run the below command to add this to the initramfs of the server.
# sudo dracut -f -v
Method # 2 : New Partition on OS disk
1. Create a new partition on the original disk, this only applies if there is some free space on it, free space can be check with the following command.
$ sudo parted /dev/sda print free
Model: ORACLE BlockVolume (scsi)
Disk /dev/sda: 50.0GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
17.4kB 1049kB 1031kB Free Space
1 1049kB 211MB 210MB fat16 EFI System Partition boot
2 211MB 8801MB 8590MB linux-swap(v1)
3 8801MB 50.0GB 41.2GB xfs
50.0GB 50.0GB 1032kB Free Space ### Free space
2. If there is free space, create a new partition on the original disk with the following commands.
$ sudo gdisk /dev/sda GPT fdisk (gdisk) version 0.8.10 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): n Partition number (4-128, default 4): 4 First sector (34-98566110, default = 97675264) or {+-}size{KMGTP}: ### Press 'Enter' for default Last sector (97675264-98566110, default = 98566110) or {+-}size{KMGTP}: Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): L 0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE 3000 ONIE boot 3001 ONIE config 4100 PowerPC PReP boot 4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS 7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved 8200 Linux swap 8300 Linux filesystem 8301 Linux reserved 8302 Linux /home 8400 Intel Rapid Start 8e00 Linux LVM a500 FreeBSD disklabel a501 FreeBSD boot a502 FreeBSD swap a503 FreeBSD UFS a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID a580 Midnight BSD data a581 Midnight BSD boot a582 Midnight BSD swap a583 Midnight BSD UFS a584 Midnight BSD ZFS a585 Midnight BSD Vinum a800 Apple UFS a901 NetBSD swap a902 NetBSD FFS a903 NetBSD LFS a904 NetBSD concatenated a905 NetBSD encrypted a906 NetBSD RAID ab00 Apple boot af00 Apple HFS/HFS+ af01 Apple RAID af02 Apple RAID offline af03 Apple label af04 AppleTV recovery af05 Apple Core Storage be00 Solaris boot bf00 Solaris root bf01 Solaris /usr & Mac Z bf02 Solaris swap bf03 Solaris backup bf04 Solaris /var bf05 Solaris /home bf06 Solaris alternate se bf07 Solaris Reserved 1 bf08 Solaris Reserved 2 bf09 Solaris Reserved 3 bf0a Solaris Reserved 4 bf0b Solaris Reserved 5 c001 HP-UX data c002 HP-UX service ea00 Freedesktop $BOOT eb00 Haiku BFS ed00 Sony system partitio ed01 Lenovo system partit Hex code or GUID (L to show codes, Enter = 8300): 8200 ### Choose Linux Swap type. Changed type of partition to 'Linux swap' Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y OK; writing new GUID partition table (GPT) to /dev/sda. Warning: The kernel is still using the old partition table. The new table will be used at the next reboot. The operation has completed successfully.
$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 98566144 sectors, 47.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 25B6B886-EA7F-4DE1-9260-979D9F34D17F
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 98566110
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00 EFI System Partition
2 1050624 17827839 8.0 GiB 8200
3 17827840 97675263 38.1 GiB 0700
4 97675264 98566110 435.0 MiB 8200 Linux swap ### New Partition
3. Run the following command to reload the changes on the partition table.
$ sudo partprobe /dev/sda
4. Create a new swap area on the new partition that had been just created on the original disk.
$ sudo mkswap /dev/sda4 Setting up swapspace version 1, size = 1020 KiB no label, UUID=eeb4e9e0-386f-4fbd-919a-130e2c17079e
5. Add the swap entry to the /etc/fstab file as indicated below on this step:
UUID=1a50efd3-ddc6-4dab-b3ad-xxxxxxxx swap swap defaults,_netdev,x-initrd.mount 0 0
6. Load the new swap space that had been created for the Instance.
$ sudo swapon -a
7. To list the swap devices run the below command.
# sudo swapon -s
Method # 3 : Create a swap file.
1. Create a swap file on the current File system for example on root, for this a new Directory can be created.
$ sudo mkdir /swap
2. Create a new file into this new directory, in this example a new file for 2Gb is create.
$ sudo dd if=/dev/zero of=/swap/swapfile1 bs=1M count=2048
3. Create a new swap area on the file that has been created.
$ sudo mkswap /swap/swapfile1
4. Change the permissions on the file.
$ sudo chmod 600 /swap/swapfile1
5. Add the swap partition to the /etc/fstab file as indicated below on this step:
/swap/swapfile1 swap swap defaults 0 0
6. Load the new swap space that had been created for the Instance.
$ sudo swapon -a
7. To list the swap devices run the below command.
$ sudo swapon -s
The new swap area that the instance will have available can be checked with this command below.
$ sudo free -m
total used free shared buff/cache available
Mem: 14763 256 11898 65 2608 14102
Swap: 12661 0 12661 ### Total Swap area