3

I read from various sources that creating swap partitions on SSDs is not recommended. I have an SSD and an HDD, and 4 GB RAM. Is it possible to create the root and home partitions on the SSD, but have the swap partition on the HDD? If is it possible, should I do this?

dnaik
  • 283
  • 1
    It is possible, but makes no sense. – Pilot6 Oct 29 '20 at 09:11
  • @Pilot6 why not? I don't know a lot about this, but having swap memory will prevent my RAM from getting overloaded won't it? – dnaik Oct 29 '20 at 09:19
  • 3
    Because HDD is slow. And fears about swap on SSD are obsolete. – Pilot6 Oct 29 '20 at 09:20
  • Ubuntu can have both swap files and multiple swap partitions running at the same time. They can be on different disks. you can confirm this using free and swapon -s. – C.S.Cameron Oct 29 '20 at 10:00
  • 1
    If fiddling with partitioning and two drives I'd suggest the other way round - swap on SDD and root on HDD. Swap performance is key to the whole system performance therefore it should be placed on the fastest media available. An even better aproach would be to have boot, root, tmp and swap on sdd and other filesystems like var, home, opt, etc. on HDD. This way even if you remove HDD the bare system is still bootable in recovery mode. – Pawel Debski Oct 29 '20 at 11:29

2 Answers2

2

On new installations of Ubuntu 17.04 and later a swap file is created by default instead of a swap partition. Among other things this saves valuable disk space on the SSD for users who have SSDs. So we don't put a swap partition on an SSD or on a HDD anymore. The swap file is located in the same partition where the root filesystem is located. On my computer which has 32GB RAM the swap file is the default size, only 1.5GB.

Since the advantage of a swapfile is that it is easy to resize, you may want to use SwapSpace to manage the size of your swapfile. SwapSpace is a small, stable system add-on that continuously and automatically adapts available virtual memory space to your actual memory needs. It claims disk space for use as swap space when needed, and frees it up for use by the filesystem when it is not needed. SwapSpace can be installed with sudo apt install swapspace in all currently supported versions of Ubuntu.

karel
  • 114,770
  • karel, what happens when we do manual partitioning and we do create a swap partition that is added to fstab for automatic mounting? Does the system use that partition automatically instead of the swap file? Does it use both? Does it still use the swap file ignoring the partition? – Stormlord Oct 29 '20 at 10:19
  • 1
    A swap file is the default in new installations of Ubuntu 17.04 and later, but the choice of swap file or swap partition or both is configurable by the user. – karel Oct 29 '20 at 10:22
  • I thought that I understood that swap takes up valuable space no matter where it is located, and that it works faster on a SSD than on a HDD, as in recovering from hibernation. Enabling hibernation is a little more complex with a swapfile than with a swap partition. – C.S.Cameron Oct 29 '20 at 10:22
  • @karel: I don't think you will be doing much hibernating with 1.5GB of swap and 32GB RAM. – C.S.Cameron Oct 29 '20 at 10:58
  • @karel: I think adjusting swapfile size is something you need to do yourself. I think Ubuntu creates a 1.5GB swapfile for all installations. Perhaps have a look at my link. – C.S.Cameron Oct 29 '20 at 11:10
  • Run sudo apt install swapspace to enable dynamic swap resizing. swapspace is in the default repositories for all versions of Ubuntu from 12.04-21.04. Small, stable system add-on that continuously and automatically adapts available virtual memory space to your actual memory needs. Claims disk space for use as swap space when needed; frees it up for use by the filesystem when not needed. – karel Oct 29 '20 at 11:14
  • Thanks @karel, That sounds very handy, will try it right now. – C.S.Cameron Oct 29 '20 at 11:28
  • @karel: Swapspace site says: N.B. If you want suspend and resume to work you will still need a static swapfile!... I have noy manaed to install Swapspace yet. – C.S.Cameron Oct 30 '20 at 03:29
  • @karel and the size of the swap file changes dynamically automatically according to Ubuntu's operational requirements ... - dynamically automatically? - first time I read that - is there a link or post that confirms that? - all the tutorials I read about to resize or increase the swap file is based with a manual manipulation – Manuel Jordan Apr 06 '22 at 22:35
  • @ManuelJordan Thanks for commenting. I replied to your comment by editing my answer. – karel Apr 07 '22 at 01:28
0

What Swap

Ubuntu can have both a swap file and multiple swap partitions running at the same time. They can be on different disks. you can confirm this using free and swapon -s

The Swap partitions can be on SSD, HDD, SD and USB. Ubuntu will use them all.

Whether or not to do this is beyond the scope of this answer. Generally the faster the media, the faster the swap operation.

Recovery from hibernation will be faster if swap is on SSD rather than HDD.

For more information on hibernation using swapfiles see: How to Handle Full Install USB and Swap Space

C.S.Cameron
  • 19,519