0

I am reading some tutorials about swap for Ubuntu and in general for other distributions. Mostly from:

From there, it refers to other valuable tutorials of the same domain, of course all about swap, among them the information is well covered and very well explained.

According with them: if the PC/Laptop has either 8GB or less is recommended use swap how a dedicated partition - it normally x2 of the current RAM capacity available (mostly when Hibernating is applied). Until here I am ok for some laptops and old PC Desktops. Be aware, they have HDD about their hard-disks.

Something of my concern is that use swap impacts the SSD's lifespan, so is suggested use swap file instead. It is suggested for other scenario about to avoid resize the swap partition

So until here swap file is valuable for:

  • Accomplish a manual administration about the swap size without a partition being involved and to avoid harm the SSD lifespan

So I want to know if is a recommendable apply the swap file approach for these PC/Laptops with either 4GB and 8GB of RAM - currently they have HDD but some of them would be upgraded to SSD when themselves passed/gone away

The goal is have one standard approach and avoid use the swap partition. I think the manual administration is flexible than a raw/fixed partition.

Therefore:

  • Is wise and safe use swap file for PCs/Laptops of 8GB or less?

it without matter if the hard disks are currently either HDD or SSD.

About SSD Lifespan

This case is mentioned here:

muru
  • 197,895
  • 55
  • 485
  • 740
Manuel Jordan
  • 1,768
  • 7
  • 30
  • 50
  • 1
    There are some recent reports that finds the SSDs are as reliable as HDDs. If this is true, you don't have to worry about the lifespan of SSDs. The default installation of Ubuntu does not create swap partition anymore. It creates swap file instead. So, I would say it is "safe" to use swap files. I am going to vote to close this question as it seeks opinion based answers, which are off-topic here. – user68186 Mar 28 '22 at 18:46
  • 1
    Your question is not very clear. But, I don't think the choice of swap partition or swap file has anything to do with the disk being an HDD or SSD. And also the decision to use or not to use swap is not related to the amount of RAM installed, but to the use purpose of the computer. – FedKad Mar 28 '22 at 19:00
  • @user68186 The page you linked to doesn't give much in the way of specifics, but the differences between SLC, MLC, and QLC will impact performance and longevity. Endurance will also depend on the amount of over-provisioning. You really need to look at the specs on the SSD to make reliability predictions. – doneal24 Mar 28 '22 at 19:39
  • Thanks for the comments - The question is clear - and the answer would be a Yes or Not according for each hard drive type with a link reference - so it is not opinion-based. BTW - I added two links about this situation for the ssd case – Manuel Jordan Mar 28 '22 at 21:23
  • @user68186 - about "The default installation of Ubuntu does not create swap partition anymore" - is there an official post that covers or announces that "new" feature or approach? – Manuel Jordan Mar 28 '22 at 21:51
  • 1
    Since Ubuntu 17.04 a swap file is the default for new installations. So it will be 5 years next month. – user68186 Mar 28 '22 at 22:22
  • Thanks for the feedback – Manuel Jordan Mar 28 '22 at 22:35
  • 1
    There are 2 questions here: swap or not (what do you want to do when your programs request more RAM than is available? Run the dreaded OOM-Killer to select and kill a RAM-using process, or slow down a little? Can't hibernate without swap.). HDD or SDD? IMHO, modern HDDs are fast enough, so avoid SDD worries. – waltinator Mar 29 '22 at 01:40
  • @waltinator swap is going to be used, it is a fact because the PC/Laptops have 4GB to 8GB - the point is if is wise and safe use swap file or not either for HDD or SSD – Manuel Jordan Mar 29 '22 at 13:20
  • @user68186 consider put your comment how an answer to mark it. – Manuel Jordan Mar 29 '22 at 13:21
  • Does this answer your question? Should the Swap be this large? – karel Apr 01 '22 at 08:45

3 Answers3

1

Personally i prefer swap files, for the same reason you already stated. Creating a smaller or larger swap file is much easier than resizing a partition.

One of my PCs has 8 GB RAM, is running Ubuntu 20.04, and has no swap space at all. You might want to consider this as an option. Depending on what you are doing with your PC, you might be fine just without swap space.

If you are worried about your SSD wearing out due to swap activity, you can reduce this by lowering the swappiness. See this FAQ.

Sheldon
  • 457
  • If you throw swap onto an LVM partition, resizing is just as easy as if it were a swapfile. The question on if swap if necessary is another discussion. – doneal24 Mar 28 '22 at 19:28
  • 1
    @doneal24, am i missing something about doing this with LVM? I just tried that, and it doesn't look easy to me.

    To be able to create a swap-LV, i need to downsize my root-LV, because the current root-LV takes 100% of its VG. But lvresize wants to unmount "/" for this. Is this even possible in a running system?

    – Sheldon Mar 28 '22 at 20:10
  • 1
    If you have allocated 100% of your VG then the process is more difficult. If you have allocated 100% of your root partition the process is just as difficult. You'll need to boot into rescue mode or through a live cd to resize the root lv. – doneal24 Mar 28 '22 at 20:20
  • 1
    EVERY machine needs swap, either a /swapfile or swap partition, even with 8G RAM. If you don't hibernate, then in your case, a 4G swap would be fine. – heynnema Mar 29 '22 at 01:23
  • 1
    Also, lowering vm.swappiness doesn't completely eliminate swapping. Modern SSDs handle swapping just fine. – heynnema Mar 29 '22 at 15:33
  • @heynnema "Modern SSDs handle swapping just fine" can you expand a little more the idea? – Manuel Jordan Mar 30 '22 at 13:30
  • 1
    @ManuelJordan The accepted answer in your second link is pretty good. The big difference in older SSDs vs modern SSDs, is something called "wear leveling". This tries to use difference parts of the SSD memory, so as not to wear out singular spots of SSD memory. – heynnema Mar 30 '22 at 13:52
1

The Default in New Install

Ubuntu uses a swap file in all new installation by default since version 17.04.

Ubuntu installation does not change this default based on whether you install Ubuntu on a SSD or a HDD.

Therefore I conclude that it is safe and wise to use a swap file with a SSD and a HDD.

Other Issues

SSDs are Fragile

There is a lot of information on the web on how SSDs are fragile and will breakdown with excessive writes (and reads). There is a recent report that finds the SSDs are as reliable as HDDs. The specifics are sparse and this may only be strictly true for enterprise/server grade drives.

Alternately one can look at original manufacturer warranties as another metric of reliability of SSDs. the consumer grade (cheap ones) SSDs and HDDs come with 3 years of warranty. This tells me for normal use the issue of an SSD breaking down may not be a problem.

How Much Swap

This topic has been hashed to death as well. Here is my two-cent's worth. The swap size is often linked to the RAM size with various rules of thumbs. What is often lost in translation is the size of swap depends on the use of the computer.

I will give you a personal example. I have a desktop with 32GB of RAM and 8GB swap (in SSD). Yet once (and only once) I managed to run out of swap. I was working with a number of large datasets in R. The code I wrote kept on loading various datasets in RAM without unloading them when they were not needed anymore. In this case, it was just bad coding practice on my part. My stopgap solution to add another 64GB of swap and run the "bad" code again. This time it worked and I got the results I needed. I fixed the code later.

Hope this helps

user68186
  • 33,360
  • Thanks for extra information - I did do more research about this. If the tutorials were written until 2020 it recommended not to use SSD - since 2021 so far SSD is acceptable - I am assuming the size of swap for either partition or file are based as according the classic rules (normally x2 of the current ram if it is less or equals than 8GB) – Manuel Jordan Mar 30 '22 at 13:38
1

One more little-known use of swap: When you install a newer executable while the old one is still running, the old image can no longer be unloaded from memory, and it has no disk backing. The kernel will relocate the old executable image to swap, so it can be memory managed normally. When the old executable finally finishes executing, the memory and the swap space backing it are freed.

Without swap, the old executable would either have to call all its pages into memory, or it would have to be killed. Otherwise it would have to be stopped in order to be replaced, and the system would have to be brought to single user mode to do package management.

Swap lets you install new packages while services are running, and to wait until installation is over to bounce the service to pick up the update.

Source:https://haydenjames.io/linux-performance-almost-always-add-swap-space/

Joepie Es
  • 1,460
  • Huh? re: "The kernel will relocate the old executable image to swap". Executable images are not relocated to swap... least recently used pages of memory are moved in/out of swap. System updates will require a reboot if software is updated that requires it, to use the new software. This whole answer is kind of wrong. Sorry. – heynnema Mar 29 '22 at 15:29
  • @heynnema Please make this comment also on the source-page. That is the last answer /comment on the page and has been for 4 years. Enlighten them with your knowledge. – Joepie Es Mar 29 '22 at 19:50
  • The original article is pretty good. I still question the last comment that you copied into your answer. Also, did you read the 4 reply comments that followed the part that you took? I question that whole last thread. – heynnema Mar 29 '22 at 20:29