4

I opened Disk Usage Analyzer and the second biggest item, was swap.img.Right clicking with the mouse coudln't even open it externally.Is it like a backup file for Ubuntu? Can i safely remove it, without having any issue or i just have to leave it there as it is. I am relatively new to this os, sorry

N Papa
  • 43

3 Answers3

8

In modern Ubuntu, when you install the system it's typical to create a swap file. A swap file is the equivalent to the Windows page file, in which it's an allocated file space designed to act as temporary storage for when your RAM is full or when there's data not necessary to be live in RAM and is put to the system's "virtual memory" which tends to be much slower than actual RAM. However, it is treated the same way as memory - data is removed / cleared / 'freed up for overwriting' the same way as chunks of RAM are handled within the system.

Historically, this was done with a "swap partition" where you'd have dedicated space on the hard drive as a partition to be your "swap space" (typically equal to RAM in order to properly hibernate your computer if you have it enabled). In more modern Ubuntu, this was switched to use an allocated raw file instead on disk as the swap space.

Thomas Ward
  • 80,112
  • You can read https://www.techtarget.com/searchwindowsserver/definition/swap-file-swap-space-or-pagefile (i'm not affiliated with them) to understand a bit more about how swap files (or as windows calls them, page files) work and how virtual memory works as well. – Thomas Ward Mar 12 '25 at 18:39
  • This does explain what it is, and suggest that it's better to leave it be, but is it safe to kill it? What happens to data stored in swap if the process is killed? – sayanel Mar 13 '25 at 07:22
  • @sayanel same way processes in memory behave - data gets cleared out, though at a slower rate. – Thomas Ward Mar 13 '25 at 15:44
  • 1
    In fact i was only curious about what it is, rather than wanting to delete it. Know i understood its basic usage it is wise to leave it as it is. Thanks for the answers <3 – N Papa Mar 13 '25 at 17:04
2

The swap.img file exists so that your computer can use the disk storage for additional memory if your RAM runs out. It is part of the Ubuntu system, and serves a valuable purpose.

As other answers mention you can remove or reduce the size of this file, but it is not advised to do so unless you understand the purpose of the file and the consequences of modifying it.

If you need more disk space and want to consider removing the file, here is a starting point for some reading material about swap so you can make an informed decision.

https://help.ubuntu.com/community/SwapFaq

izzy
  • 156
  • In fact i was only curious about what it is, rather than wanting to delete it. Know i understood its basic usage it is wise to leave it as it is. Thanks for the answers <3 – N Papa Mar 13 '25 at 17:04
1

Can i safely remove it, without having any issue or i just have to leave it there as it is.

You can turn it off with swapoff swap.img. It'll move what's allocated there to RAM. Then you can rm it. You might want to also remove/comment-out its entry in /etc/fstab. The entry there would be so that its turned on during startup.

This is Linux-generic. IDK if Ubuntu does something additional, but probably not.

If you ever want it back, just:

fallocate -l $desired_swap_size swap.img
mkswap swap.img
swapon swap.img

and possibly re-add its entry to /etc/fstab so that it's turned on automatically at startup.

All commands are as root.

JoL
  • 1,468
  • 1
    Not recommended to a new user. – vanadium Mar 13 '25 at 11:00
  • In fact i was only curious about what it is, rather than wanting to delete it. Know i understood its basic usage it is wise to leave it as it is. Thanks for the answers <3 – N Papa Mar 13 '25 at 17:04