Swap area for ubuntu installation

I have 4 GB RAM on my laptop, and i allocated 3 GB for swap area while installing.So is having that much swap area harmful for my laptop? And if yes, then how can i change the size of swap area.How much swap area should i have allocated? When i run command

swapon --show

It shows

NAME      TYPE SIZE USED PRIO
/swapfile file   2G  59M   -2

But here, it shows only 2 GB, .So Does that mean swapfile and swapspace different? I don't know what to do. I am new to Ubuntu. Please help.


With only 4G RAM, I'd recommend a 4G swap.

Do this to enlarge your current 2G swap...

Note: Incorrect use of the dd command can cause data loss. Suggest copy/paste.

sudo swapoff -a           # turn off swap
sudo rm -i /swapfile      # remove old /swapfile

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

sudo chmod 600 /swapfile  # set proper file protections
sudo mkswap /swapfile     # init /swapfile
sudo swapon /swapfile     # turn on swap
free -h                   # confirm 4G RAM and 4G swap
reboot                    # reboot and verify operation

Also, do sysctl vm.swappiness, and if it returns anything but "60", let me know, and I'll give you further instructions.


To Enlarge Swap Space:

sudo swapoff -a
  • Delete your existing swap file located at root

  • Create the new swap file:

    sudo fallocate -l XG /swapfile

Where X is the swapfile size in GB (4GB in your case)

sudo mkswap /swapfile

sudo chmod 0600 /swapfile

sudo swapon /swapfile
  • Reboot:

    sudo reboot

To enable Hibernation (optional):

  • Edit /etc/default/grub to add resume location and offset to grub.cfg:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX resume_offset=XXXXX"

  • Use UUID from root.

  • Use offset from sudo filefrag -v /swapfile

cscameron@cscameron-T:~$ filefrag -v /swapfile Filesystem type is: ef53 File size of /swapfile is 4819255296 (1176576 blocks of 4096 bytes) ext: logical_offset: physical_offset: length: expected: flags: 0: 0.. 0: 303104.. 303104: 1: 1: 1.. 2047: 303105.. 305151: 2047: unwritten 2: 2048.. 4095: 311296.. 313343: 2048: 305152: unwritten

  • resume_offset=303104

  • Update GRUB

    sudo update-grub

  • Test hibernation

    sudo systemctl hibernate

  • There is a slight possibility of getting holes in a swapfile when creating it with fallocate. /var/log/syslog can be searched for swapon: swapfile has holes to ensure there will be no data loss.

A hibernate button can be added using gnome extensions.

Proof of concept that hibernation works with a swapfile

enter image description here Showing swapfile, resume and UUID's enter image description here Showing hibernation popup enter image description here Showing resume from UUID is root partition and not from swap partition