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
  • 4

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
  • 78,878
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
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,458