How to Allocate More Space to Swap and Increase its Size Greater than Ram?
You just want to increase the swap size on your system using the space from sda2
. Your sda2
/dev/sda2 104G 74G 25G 75% /
You can add additional swap space to your system by using swap file created on /
that will utilize your sda2
. Just do:
dd if=/dev/zero of=/swapfile bs=20480 count=1M
and then do:
sudo mkswap /swapfile
sudo swapon /swapfile
and check, you swap space will increase by that amount using free -m
and yes , to enable it at boot time add the entry in /etc/fstab
/swapfile none swap sw 0 0
Memory management
To display swap usage summary by your device:
$ swapon -s
To displays the amount of your free and used physical and swap memory:
$ free -h
To preallocate space to /swapfile, you can use the line below:
$ fallocate -l 20G /swapfile % OR $ dd if=/dev/zero of=/swapfile bs=20480 count=1M
Change permission and create/activate swap
$ chmod 600 /swapfile $ mkswap /swapfile $ swapon /swapfile
You can also improve your file security by changing your file attributes using
chattr
. I recommend you read its manual page, or read this guide on Tecmint website.Verify it is enabled by viewing the output of the command
cat /proc/swaps
, usefree
command, or$ swapon -s
To enable it at boot time, edit
/etc/fstab
(static information about the file system) to include the following where the fields arefs_spec
,fs_file
,fs_vfstype
,fs_mntops
,fs_freq
andfs_passno
, about which you can read byman 5 fstab
:$ vi /etc/fstab /swapfile none swap defaults,discard 0 0
- In the fourt parameter fs_mntops, do not use just
sw
when swapping to an SSD butdefaults,discard
such that the memory blocks are trimmed each time at startup, see the answer here about How to Do Error-trapping and Swapoff if Error/Warning? Thediscard
option definitely works on partitions.
- In the fourt parameter fs_mntops, do not use just
You question, "How to Allocate More Space to Swap and Increase its Size Greater than Ram?", does not say anything about changing the way your system is set up.
Your fdisk and free output tells us:
- You have a a partition (/dev/sda3) dedicated to swap. If you just resize that partition you should not need to change anything in your system to use the extra space. (/etc/fstab).
- You have a partition (/dev/sda2) that has the OS on it. This partition has 26G free space. If you shrink this partition by 20G the OS will have 5G left to grow and use.
The swap partition is not a extended partition, this makes it a little bit easier to increase the size.
Tools required:
- Bootable OS with gparted
Look here for one location of the last free version of Parted Magic
Instruction for resizing partitions with gparted
- boot into another OS that has gparted tool
- In gparted; resize OS partition (/dev/sda2). Shrink by amount to donate to swap space. This could take a lot of time because all of the data that is in the space being freed up will need to be moved by gparted.
- In gparted; resize swap partition (/dev/sda3). Move and extend to include all free space.
- reboot into sda2 OS.
- verify swap partition (/dev/sda3) is being used.
Remember that you are changing you disk and partitions on a low level and that any errors or crashes could damage you data.
Backup you data first.