1

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.

4
  • 1
    4GB swap for 4GB RAM is normal if you want to hibernate. Jun 8, 2020 at 10:09
  • It is possible that there is a 2GB swapfile plus a 3GB swap partition on your HDD and that the swap partition has been turned off. Open Disks and check if you see a swap partition. Jun 8, 2020 at 10:50
  • I don't see any swap partition on my Disks.Does that mean swap partition has been turned off automatically? Does the size of swap partition affect the laptop hardware?And one more question, if i don't want to enable hibernation, then my current swap file size is good , i don't need to change it, right? I am new to these things, Please forgive me for dumb questions, if any.
    – wrc hunks
    Jun 8, 2020 at 12:49
  • You have a swap file, not a swap partition. Size of swap file should not affect hardware. Size should be good, you can occasionally run free to check that swap is not all used up. Jun 8, 2020 at 13:33

2 Answers 2

1

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.

7
  • Do you really want to turn a new user, with a rep of 1, loose with dd? Jun 9, 2020 at 0:35
  • @C.S.Cameron Hence, the warning note, and the suggestion to use copy/paste.
    – heynnema
    Jun 9, 2020 at 0:49
  • Not sure whether or not to add this comment. Your warning is nice, but after using Ubuntu for fourteen years and seeing similar warnings dozens of times, I managed to accidentally zero my laptop's SSD using dd just last year. Jun 9, 2020 at 5:07
  • What can possibly happen if i don't enlarge the swapfile size?
    – wrc hunks
    Jun 9, 2020 at 5:15
  • @wrchunks With only 4G RAM, and too small a swap file, you may run out of RAM, and find your system freezing frequently. This would occur when you have a number of tabs open in Firefox/Chrome, or have many applications running at the same time.
    – heynnema
    Jun 9, 2020 at 12:28
1

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

5
  • fallocate is not recommended to create swap files. dd is the suggested tool. See man swapon for details. See my answer.
    – heynnema
    Jun 8, 2020 at 22:01
  • @heynnema Actually fallocate is recommended quite often, it is the first method recommended by Google. dd is not recommended for new users, I would rather have holes in my swapfile than a dead HDD. If a person is worried about holes they can search their system log for swapon: swapfile has holes. That is much faster than restoring a system from backup. help.ubuntu.com/community/SwapFaq Jun 9, 2020 at 0:33
  • @heynnema I am currently using the method I posted here, I have checked it a half dozen times with 18.04 and 20.04 before posting. I have even added a hibernate button using gnome extensions. You are welcome to try it yourself, only takes ten minutes. resume_offset is necessary with a swapfile. Jun 9, 2020 at 1:07
  • @heynnema: Proof of concept for hibernating with swapfile added. Jun 11, 2020 at 4:14
  • Thank You so much. It worked. I've been looking for a way to Hibernate Ubuntu for a really long time.
    – an4s911
    Mar 27, 2021 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .