69

I have laptop with 8 GB RAM and 1TB HDD. I have swapfile which is 2 GB (Ubuntu 18.04 uses a swapfile instead of a separate swap partition by default) and I want to increase it to use hibernation.

I want to increase it from 2 GB to 16 GB. Here is a screenshot of GParted:

GParted screenshot

I tried to increase it with fallocate -l 16G but it did not work.

Also there is the image from free -m:

<code>free</code> output

David Foerster
  • 36,264
  • 56
  • 94
  • 147
Miroslav
  • 979
  • This is for swap file, I want to increase swap partition. I don't think that this thread is duplicating with other's. – Miroslav Sep 15 '18 at 11:54
  • From the screenshot you posted, there is no swap partition. I guess you already have a swapfile. AFAIK Ubuntu 18.04 does create swapfile by default and no swap partition. – Thomas Sep 15 '18 at 12:01
  • 2
    Oh yes, actually from swapon --show it it indeed /swapfile. I will try this guide. – Miroslav Sep 15 '18 at 12:04
  • If you have 8GB of RAM you only need 8GiB for swap. But why do you want hibernation. Ubuntu boots very fast without hibernation. – oldfred Sep 15 '18 at 15:01
  • If you want to add extra swap file at will, there's a script for that: https://askubuntu.com/a/931170/295286 – Sergiy Kolodyazhnyy Sep 28 '18 at 06:57
  • 1
    Could you please post text files, dialogue messages, and program output listings as text, not as images? To achieve the latter two you can either 1) select, copy & paste the dialogue text or terminal content or 2) save the program output to a file and use that. Longer listings (the editor will tell you what’s too long) should be uploaded to a pastie service and linked to in the question. Thanks. – David Foerster Sep 28 '18 at 08:24
  • @oldfred hibernation is nice on laptops because it doesn't use any power (unlike sleep) and your programs are still running as you left them. Also good for scheduled power outages or extended power outages in general if you have a UPS. – Chai T. Rex Sep 28 '18 at 09:02
  • I and others have recently installed Ubuntu 18.04, and it created a swap partition. In my case (8GB RAM machine), the swap partition was only 1GB, which is very low. Like the OP of the linked question, I've been running into out of memory errors with just Firefox and an IDE open. Given that Ubuntu still has no mechanism whatsoever to warn of running out of memory, this choice of swap is IMO stupid. – Dan Dascalescu Dec 03 '19 at 16:35

2 Answers2

160

From Ubuntu 18.04 onwards, a swapfile rather than a dedicated swap partition is used (except when LVM is used). The swap file is named swapfile. To change the size of this swap file:

  1. Disable the swap file and delete it (not really needed as you will overwrite it)

    sudo swapoff /swapfile
    sudo rm  /swapfile
    
  2. Create a new swap file of the desired size. With thanks to user Hackinet, you can create a 4 GB swap file with the command

    sudo fallocate -l 4G /swapfile
    

    In this command, adjust 4G to the size you want.

    Alternatively, you can use dd but feeding the correct parameters requires some calculations. If you want to make a 4 GB swap file, you will need to write 4 * 1024 blocks of 10242 bytes (= 1 MiB). That will make your count equal to 4 * 1024 = 4096. Create the file of this size with the command

    sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
    
  3. Assign it read/write permissions for root only (not strictly needed, but it tightens security)

    sudo chmod 600 /swapfile
    
  4. Format the file as swap:

    sudo mkswap /swapfile
    
  5. The file will be activated on the next reboot. If you want to activate it for the current session:

    sudo swapon /swapfile
    

You can check the swap that is available with the command swapon -s (no root permissions needed).

vanadium
  • 88,010
  • 2
    this works like a charm, but unfortunately it does not survive reboots on my setup with an encrypted partition: nvme0n1p3_crypt -> ubuntu--vg-root / ubuntu--vg-swap_1 any idea on how to persist this? – rypel Sep 03 '19 at 08:13
  • 1
    Is bs=1M the fastest block size? what happens if I change that? – Ben Winding Oct 25 '19 at 04:18
  • @rypel you need to make an entry in /etc/fstab – heynnema Nov 11 '19 at 20:34
  • 2
    You need to add text to your answer that includes the mods required to /etc/fstab. – heynnema Nov 11 '19 at 20:35
  • 1
    "From Ubuntu 18.04 onwards, a swapfile rather than a dedicated swap partition is used." - that doesn't seem to be the case any more, as seen in this question, plus my own experience with installing Ubuntu 18.04.3 last week. Though where exactly the swap partition is, beats me. – Dan Dascalescu Dec 03 '19 at 17:36
  • Fair enough. I tried to undo the downvote, but I get "Your vote is now locked in unless this answer is edited.". Will upvote again after you make an edit. – Dan Dascalescu Dec 03 '19 at 17:39
  • 1
    In step2 to skip all the calculation, you can do sudo fallocate -l 4G /swapfile. Just change the value for 4G to whatever size you want, 1G, 2G, 3G... etc. – Hackinet Feb 24 '21 at 07:58
  • 1
    @Hackinet, Thank you very much, way more elegant indeed! I have included that in the answer. – vanadium Feb 24 '21 at 08:27
  • This is great! One thing I'd note is that swapoff will increase memory pressure if the swap space is in use when you run it; in other words, it tries to move stuff out of swap space into main memory. So you shouldn't try to do this when you're actually running low on memory, otherwise you'll get hit with the OOM killer while trying to increase your swap space :-) – Dan Feb 26 '21 at 03:02
  • 1
    Worked for ubuntu 20.04 as well, thank you – partizanos Apr 15 '21 at 16:43
6

From the man mkswap it is recommended to use the dd command as demonstrated in @vanadium post.

If  you  don't  know  the  page  size  that  your  machine uses, 
you may be able to look it up with 
"cat /proc/cpuinfo" 
(or you may not – the contents of this file depend on architecture and kernel version).

   To set up a swap file, it is necessary to create that file before   
   initializing  it  with  mkswap,  e.g. using a command like

          # fallocate --length 8GiB swapfile

   Note  that  a  swap  file must not contain any holes.  Using cp(1) to  
   create the file is not acceptable.
   Neither is use of fallocate(1) on file systems that support preallocated 
   files, such as XFS or ext4,  or on  copy-on-write  filesystems like btrfs.  

   It is recommended to use dd(1) and /dev/zero in these cases.
   Please read notes from swapon(8) before adding a swap file to copy-on- 
   write filesystems.

And here the notes of man swapon

NOTES
       You should not use swapon on a file with holes.  This can be seen in
       the system log as

              swapon: swapfile has holes.

       The swap file implementation in the kernel expects to be able to write  
       to the file directly, without the assistance  of the filesystem.  This 
       is a problem on preallocated files (e.g.  fallocate(1)) on filesys‐
       tems like XFS or ext4, and on copy-on-write filesystems like btrfs.

       It is recommended to use dd(1) and /dev/zero to avoid holes on XFS
       and ext4.

       swapon may not work correctly when using a swap file with some  
       versions of btrfs.  This is due to  btrfs being  a copy-on-write 
       filesystem: the file location may not be static and corruption can 
       result.  
       Btrfs actively disallows the use of swap files on its filesystems
       by refusing to map the file.

       One possible workaround is to map the swap file to a loopback device.  
       This will allow the filesystem to determine the mapping properly but  
       may come with a performance impact.

       Swap over NFS may not work.
abu_bua
  • 10,783