39

I'm running Ubuntu 20.04, and I'm trying to create a hibernate command. I'm following these instructions.

Step 2 says

Then check whether the swap memory you allocated is more than or at least equal to the Physical memory(RAM).

I did that, and my /swapfile is 2 GB. I have 16 GB RAM, so I need to increase the size of that swap file. How do I do that in Ubuntu?

3
  • Have you seen this?
    – KGIII
    Aug 4, 2020 at 18:00
  • There's also this one, relating to increasing swap under LVM and with encrypted drive: askubuntu.com/questions/1031275/…
    – jdpipe
    Jul 4, 2021 at 4:11
  • The answers below are great if your swap is a file. What if it's a partition?
    – jpcgt
    Jan 26 at 17:08

3 Answers 3

81

In sleep mode, the content of ram is kept as it is, and the computer works on a very low power mode, so as to keep the ram content intact (as ram will lose the data if power supply is cut to it). But in hibernation, the ram content is stored in the swap space, so power can be completely cut off. Hence it is recommended to have swap size as large as the ram size.

  1. First, find the swap file, disable and delete it

     swapon --show
     NAME      TYPE SIZE USED PRIO
     /swapfile file   2G   0B   -2
    
     sudo swapoff /swapfile  
     sudo rm  /swapfile
    
  2. Create new swap space of size 16 GB (16 * 1024 = 16384). bs is the block size. Basically bs * count = bytes to be allocated (in this case 16 GB). Here bs = 1M (M stands for mega, so we are assigning 1MB block size) and we are allocating 16384 * 1MB (=16GB) to swap.

     sudo dd if=/dev/zero of=/swapfile bs=1M count=16384
    
  3. Give it the read/write permission for root

     sudo chmod 600 /swapfile
    
  4. Format it to swap

     sudo mkswap /swapfile
    
  5. Turn on swap again

     sudo swapon /swapfile
    
  6. Now reboot the PC for the above changes to take place.

11
  • 5
    There is no need to delete the file or use dd. It is much faster and easier to fallocate. Why would you write zeros there?
    – Pilot6
    Aug 4, 2020 at 18:24
  • 2
    @Pilot6 Sure even that works. I remember that I used the above commands, hence I wrote that. I am sure that the OP owner just needs to get the work done, rather than quickly. Aug 4, 2020 at 18:27
  • 4
    This was quite helpful... Thanks Jul 20, 2021 at 18:02
  • 2
    Thanks Abhay Patil. Just a note: I did not need to reboot on Ubuntu 20.04 and you can put the swapfile where you want (doesn't need to be in the root folder/drive). Jul 29, 2021 at 12:26
  • 1
    @LouisGagnon but it's better to put it in the root, because if you put it, say, into /home/user, you might accidentally delete it.
    – TheEagle
    Jul 31, 2021 at 17:38
15

Below steps worked for me with fallocate approach

  1. Check for the Swap Information

sudo swapon --show

Output will be something like below,

NAME      TYPE   SIZE   USED   PRIO
/swapfile file    2G     0G    -2
  1. Deactivate the swap space

sudo swapoff /swapfile

  1. Use fallocate to instantly creates a file with the specified size.

sudo fallocate -l 16G /swapfile

  1. Set the swap space

sudo mkswap /swapfile

  1. Activate the swap space

sudo swapon /swapfile

  1. Now check for the Swap Information

sudo swapon --show

Now the output will be something like below,

NAME      TYPE   SIZE   USED   PRIO
/swapfile file   16G     0B    -2

Cheers!

2
  • 1
    If the second steps failes (sudo swapoff /swapfile) with "Killed" error message to stop swap you may need to first stop applications/services which consume large amounts of memory. You can identify them using top command or recent OOM by running grep /var/log/kern.log* -ie 'Out of memory' Sep 16, 2022 at 1:42
  • 2
    This should be the accepted answer. Mar 7, 2023 at 13:23
1
sudo su -
swapoff /swapfile  
rm  /swapfile
\# 32k -> 32GB swap size
dd if=/dev/zero of=/swapfile bs=1M count=32K
mkswap /swapfile
swapon /swapfile
Ctrl+D

You must log in to answer this question.

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