2

Recently, I clean-installed Ubuntu 20.04, with minimal installation option.
Now, when I check my swap space using the command swapon -s, I get the following output:

Filename     Type     Size     Used    Priority
/swapfile    file     2097148  268     -2

When I see the list of partitions, using gparted, I see the following:

enter image description here

Also, System Monitor app shows the swap space of 2GB (see screenshot below).

enter image description here

Clearly, I don't have a partition dedicated for swap space.

I have the following question: Is it normal for Ubuntu to not create swap partition, and instead create a swapfile?

3
  • There's nothing wrong with having a swap file on ext4 AFAIK. But since I don't use Ubuntu, I don't know if it's normal.
    – Oskar Skog
    Jul 6, 2020 at 20:00
  • Can you tell us the circumstances of the creation of the "swap file" you refer to ?
    – Cbhihe
    Jul 6, 2020 at 20:26
  • @Cbhihe, Initially I was running dual OS (Windows 10 & Ubuntu 18), then removed Windows. Then played a bit (tried different desktop environments and packages), and then removed Ubuntu 18 as well, because trying different desktop environments while logging with the same user profile caused them to somehow interfere with each other (E.g. I was seeing KDE packages when I logged in on XFCE). So, I clean-installed new Ubuntu 20.04
    – Avinash D
    Jul 6, 2020 at 21:32

3 Answers 3

1

Yes, it's normal.

To be honest the SWAP partition has never made too much sense: unlike a normal file you cannot easily delete, create, extend, or shrink it.

A separate SWAP partition is still required if you use full disk encryption but then people have already created workarounds: https://rephlex.de/blog/2019/12/27/how-to-hibernate-and-resume-from-swap-file-in-ubuntu-20-04-using-full-disk-encryption/

2
  • I have always used swap files on full encrypted disks. It works fine. In fact that's what you WANT otherwise someone could gleam data from the swap space.
    – CR.
    May 2 at 21:37
  • "the SWAP partition has never made too much sense" strictly speaking, until relatively recently having swap as a partition made lots of sense. No need to worry about its placement in the disk. Blocks don't move. It's unlikely to be deleted. No need to understand it even use a filesystem to get to swap. It would have made for a much simpler interface inside the kernel: swap starts at disk block X and continues sequentially until disk block X+N
    – roaima
    May 2 at 21:56
1

AFAIK "swap file" is a rather loose naming convention that applies to a few different things on linux platforms. Here from what you show us, it is a swap file, with exactly 2048MB or 2GB of swap space.

It is there because some process or program or user with root permission on your system, created it. It is actual swap space and may or may not be enabled. It is usually created as a supplement to the more conventional swap space mounted at boot.

You or a process can create a swap file anywhere. Here it was created on /, your root partition. You can repeat that process like so:
(here I use the dd "data dump" command.)

$ su  # enter root password.
# dd if=/dev/zero of=/extraswapfile bs=1M count=1024
# mkswap /extraswapfile
# swapon /extraswapfile    # to turn the swapfile on
# swapoff /extraswapfile    # to turn it off

To visualize your total available swap space, you can use swapon -s as you do in your question. You will see the previous swap partition's space (if any) + the 2GB of extra swap space you just enabled via the swap file.

A swap file is not persisted and disappear at reboot, so it is practical when administering swap space in an ad hoc way is needed. Rebooting then wipes your (swap) slate clean. If you want to make a swap file persistent across reboots, edit /etc/fstab to include:

/extraswapfile   none   swap   sw   0   0

But persisting a swap file is generally ill-advised. Swapping already tends to translate as a drop in performance due to increased I/O operations on disk. It is preferable to increase the size of a swap partition than to add to a supplementary, non-contiguous swap space in rhe form of a swap file. Your case seems different since you do not have an already existing swap partition.

Reference:

https://fibrevillage.com/sysadmin/439-check-and-identify-memory-and-swap-on-different-unix-linux-distros

https://www.linux.com/training-tutorials/increase-your-available-swap-space-swap-file/

For a discussion on swap area priority, also see man 2 swapon.

2
  • 1
    I think the swap space is 2GB (not 2MB as you mentioned in the answer).
    – Avinash D
    Jul 6, 2020 at 21:56
  • @PallaviDubey: Yes, you are right. My mistake.
    – Cbhihe
    Jul 7, 2020 at 5:29
-6

This is NOT normal as it's unpredicted and uncontrolled behavior. I've made a clean install (ubuntu-20.04.1-live-server-amd64) on Virtualbox without LVM, root partition only with no swap partition defined, but the swap file appeared after installation! Nobody asked Ubuntu to create a swap at all and a swapfile on a root especially, but it did! This is still not fixed in 22.04 and also leads to potentially trashing or oversizing VMs' virtual drive. The swapfile may be created even if a swap partition was defined so you will end up with both used. The size of resulting swapfile may vary and may depend on the RAM, drive or partitioning configuration! IMHO No other distro does this.

There are multiple questions on askubuntu about this and people even have problems with it! But it seems nobody cared much if it's a weird behaviour and a potential bug (see 1391554, 1327558).

# for starters who wish to remove the swapfile
sudo swapoff -a # all or specify the file
sudo nano /etc/fstab # and remove or comment the line containing swap mount
sudo rm -rf /swap.img
4
  • 5
    "This is NOT normal" and "multiple questions on askubuntu about this" - so it would seem that for this version of Ubuntu it is normal (I e. expected) behaviour. If you don't like it, create a swap partition and use that instead. I like swap either on LVM or as a file in the filesystem. (But I prefer a different distro to Ubuntu.) YMMV
    – roaima
    May 2 at 21:59
  • Swapfiles are better for SSDs, whereas a fixed swap partition can prematurely degrade SSDs. May 3 at 4:42
  • @roaima, An expected behavour is to present the user with a correct mounts configuration at the installation stage and let him do changes or accept it. The installer has a gui to setup swap space and it should display that a file mount is added. In current situation nobody asked it to add swap, but it did. You can't even select its size. If you checked askubuntu pages then you should note a guy having both partition and a swapfile problem. I've made a clean install just now with a custom paritioning containing a swap partition. Now I have both a swapfile (1.3G) and a swap partition (2G).
    – gvm
    May 3 at 7:56
  • @Greenonline, that's a good reason, yet one should confirm that the swap file will be reallocated on boot or managed somehow else. I would expect ssd firmware/internals take care on block usage nevertheless of being on partition or a swapfile. Also imagine consequences when it's done on VM with a host having SSD (that's why I worried on unasked swapfile).
    – gvm
    May 3 at 7:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.