To swap or not to swap, that is the question.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
2,361
Reaction score
2,047
Credits
18,095
No, I'm not talking about your car or your girl-friend here. I'm talking about swap space.

For the purpose of this post, I don't really care if it's a swap partition, or a swap file.
But rather do you use or need swap space at all?

According to chatGPT...

Linux uses a swap partition (or swap space) for several reasons:

  1. Virtual Memory: Swap space allows Linux to implement virtual memory. Virtual memory is a memory management technique that provides an illusion to the user that there is more RAM available than there actually is. It does this by using a combination of physical RAM and swap space on disk to store data that is not immediately needed in RAM.
  2. Improving System Stability: Swap space can help improve system stability. When RAM becomes full, Linux can move less frequently used data to the swap space temporarily, allowing the system to continue running even if RAM is exhausted. This is crucial in preventing crashes due to running out of memory.
  3. Hibernation: Swap space is also used for hibernation. When you hibernate your computer, the contents of your RAM are saved to the swap space. When you wake your computer, the contents are read back into RAM, allowing you to resume where you left off.
  4. Balancing Memory: In some cases, swap space can help balance memory usage. For example, if a process has allocated a large amount of memory but is not actively using it, Linux can move some of that data to the swap space to free up RAM for other processes that need it.
  5. Emergency Recovery: If your system encounters a severe memory issue, and the RAM is exhausted, swap space can act as a last resort to prevent a system crash. It's better to have some performance degradation due to swapping than a sudden crash.
  6. Overcommit Handling: Linux uses swap as a part of its memory management strategy, especially for handling memory overcommit. Overcommit handling allows Linux to allocate more memory than is physically available, relying on swap space to store data that may never be used.
While swap space is essential for these reasons, the amount you need depends on your system's RAM and how you use it. In some cases, systems with ample RAM may hardly use swap space at all, while systems with limited RAM or specific workloads may make more frequent use of it. You can configure and adjust the size of your swap partition to suit your specific requirements.

-----------------------------------------------------------------------------------------------

So it's not only for "running out of memory", it's also used when your computer hibernates.
I have 80GB on this computer I'm using at this moment, and I've never filled up my RAM, but I do do have
a swap partition. I didn't make it 80GB, It's about 16GB because I have two 2TB drives, so space isn't a problem.

But even though I never fill up my RAM, occasionally I see things in swap. Upon further investigation, it seems
RAM can get fragmented, this isn't nearly as big of a deal as in the old days when hard drives got fragmented files on them.

Having a swap file can help, it gives a temporary spot to store fragmented memory until your computer
cleans things up. Even if your RAM isn't full. "Random Access" memory should mean I can access all memory equally
fast, but that isn't always true,

Now, for the people who say I only have 4GB or 8GB of RAM, and I never use swap... you aren't really power users are you?
I don't think I've ever managed to have this system under 8GB in the last year or two. If I ever get down to 7GB, I make sure I
open a couple more browser tabs! :) But seriously, between my desktop, the apps, I'm running, the VMs running, and tasks in the
background, I don't think I could ever go back to an 8GB computer, I don't know how people with 4GB do it honestly.

Now if you're mostly using Linux on your home computer this next part probably doesn't matter that much, but if you're using
Linux in a business or data center, and a computer crashes, you might want to know why.

--------------------------------------------------------------------------------------

Swap space can play a role in handling system crashes and kernel dumps in a limited way, but its primary purpose is to manage system memory, and it may not directly impact the behavior of kernel dumps or system crashes. Here's how swap space can be related to these scenarios:

  1. System Crash Handling: In the event of a system crash, the kernel typically generates a core dump, which is a snapshot of the system's memory at the time of the crash. Swap space can indirectly influence this process if it's used to help manage system memory. If a system runs out of physical RAM and doesn't have enough swap space available, it may increase the likelihood of a system crash due to a memory exhaustion issue. Having sufficient swap space can prevent some memory-related crashes.
  2. Kernel Dumps: A kernel dump is a snapshot of the kernel's memory and the state of the system when the kernel encounters a critical error. Swap space doesn't directly impact the generation of a kernel dump. The kernel dump process itself is controlled by the kernel, and it is typically written to a dedicated file or partition, rather than the swap space.
It's important to note that the primary function of swap space is to provide virtual memory, handle memory overcommit situations, and improve system stability by allowing the operating system to use disk space as an extension of physical RAM. While it can indirectly help prevent certain types of system crashes caused by running out of memory, it doesn't directly influence the process of generating kernel dumps or handling system crashes. The configuration and management of kernel dumps are more closely related to the operating system's settings and crash handling mechanisms.

-------------------------------------------------------------------------------------------

Now having said all of this, I do have some computer system that are primarily for containers.
Podman, Docker, or even VirtualBox. On these system I do not run any swap. It's usually recommended that you turn swap
off when this is the primary use for a particular system. Also the containers themselves don't want to have swap enabled,
after all, these are virtual file systems on virtual disks, so having swap on swap really doesn't make sense and would hurt your
systems performance.
 


I just let the default Linux installer create the size of my swap file or swap partition.
My desktop only has 4.0 GB of memory so the installer creates a swap file of 511 MB which seems to be enough as I don't use the hibernate mode.
I also change the unreasonable level of swappiness of 60 to a more reasonable level of swappiness of 10 which creates less writes to an already old mechanical hard drive.
 
But seriously, between my desktop, the apps, I'm running, the VMs running, and tasks in the
background, I don't think I could ever go back to an 8GB computer, I don't know how people with 4GB do it honestly.
Having a computer with a lot of memory and a faster processor would be nice and have been there and done that although that computer had a power supply failure and died.
Some of us computer users as myself don't need much computing power.
I never have but two or three tabs open at a time.
I don't have no reason to use VMs.
I do web browsing and stream movies Youtube or DVD and move a few photos from my cell phone or digital camera and maybe run a business document every now and then.
So for users as myself 4.0 GB of memory works fine.
Hell a 40 GB hard drive works for what I do.
 
I've pretty much just let the install decide. In 30 some odd years, I haven't had an issues with swap, just a loose nut at the keyboard. :D
 
I've pretty much just let the install decide. In 30 some odd years, I haven't had an issues with swap, just a loose nut at the keyboard. :D
You ain't alone on the loose nut on the keyboard. :p
 
I figure the kernel is smarter than I am.

Even on systems with more RAM than I would realistically use, the kernel uses some swap. (Swap is not 'extra RAM', as many seem to think of it.)

When the kernel stops using swap (when enabled, of course), I'll stop using swap.
 
Well, I've been using Linux (& BSD) for quite a few years now, & I don't use swap!

Presently running Devuan/XFCE with just 2GB of ram, on a 1.2GHz dual core processor, & I have no problems, not even when using Firefox on the internet.

OK, I'm just an average guy, doing average things, (browsing the internet, listening to music, watching videos, playing timewaster games, etc), but why would I want/need more.... :)
 
Well, I've been using Linux (& BSD) for quite a few years now, & I don't use swap!

Presently running Devuan/XFCE with just 2GB of ram, on a 1.2GHz dual core processor, & I have no problems, not even when using Firefox on the internet.

OK, I'm just an average guy, doing average things, (browsing the internet, listening to music, watching videos, playing timewaster games, etc), but why would I want/need more.... :)
Many years ago when I first used Lubuntu 14.04 LXDE the default installer didn't create a swap partition I couldn't tell a difference.

I don't believe Easy OS or any of the Puppy Linux creates swap partitions although Puppy Linux does ask if you want to create swap.

I believe that running 100% in ram swap isn't needed I may be wrong.
 
Last edited:
I believe that running 100% in ram swap isn't needed I may be wrong.

Huh... Now that there's a nugget, when I really think about it.

Swap isn't really stored in RAM normally. So, the organization that swap provides may still be beneficial. It's still used to help manage RAM. If you read the bullet points in the OP, you'd be missing out on those benefits.

So, I'd have to guess that there's some benefit to be had.

I'd defer to the kernel. I'd try it with swap enabled (if possible). If the kernel then uses swap, I'd probably err on the side of caution and default to using swap.
 
Huh... Now that there's a nugget, when I really think about it.

Swap isn't really stored in RAM normally. So, the organization that swap provides may still be beneficial. It's still used to help manage RAM. If you read the bullet points in the OP, you'd be missing out on those benefits.

So, I'd have to guess that there's some benefit to be had.

I'd defer to the kernel. I'd try it with swap enabled (if possible). If the kernel then uses swap, I'd probably err on the side of caution and default to using swap.
Can you even install swap when running 100% live mode.
The only files that are saved are SFS files within a save file.

Aren't the "bullet points" in the OP only beneficial when installed onto a hard drive.
 
Can you even install swap when running 100% live mode.

I really don't know. I mean, a swapfile should work.

Aren't the "bullet points" in the OP only beneficial when installed onto a hard drive.

The only one I see that'd only impact an installed OS would be the hibernation benefit. You can't hibernate a live system, I don't believe. The rest would (almost certainly) still be benefits even if it's a live system.

Note: I'm not saying you must run swap. I just think it'd be interesting to see the results of doing so and what I would do if it did indeed work.

All sorts of people run distros without any swap at all, so it's not necessary. (Which isn't the same as being not beneficial.)
 
Hmm I sure don't know maybe @MikeWalsh will offer an opinion about Swap and Puppy Linux I just don't know.

It makes sense that a Swap file or Swap partition is useful.

Yeah kind of is a bit of a head scratchier. :confused:
 
Yeah kind of is a bit of a head scratchier. :confused:

Given the transient nature of a system that runs purely in RAM, it may just not be worth the effort.

Yeah, there are benefits and the kernel does use swap when it is available (even on higher end devices with gobs of RAM), it may not be enough of a benefit to bother with it.

I just checked the refurb, which only has 16 GB of RAM, and the output is:

Code:
$  free -m
               total        used        free      shared  buff/cache   available
Mem:           15873        7675         995         803        7202        7067
Swap:           2047        1039        1008


However, I've never rebooted the device since the moment it was turned on after the SSD swap.

Code:
$  uptime
 16:49:09 up 25 days,  2:00,  2 users,  load average: 1.25, 1.07, 0.94

I've observed that the system uses more swap the longer it has been running, which makes sense.
 
I set swap partition to same as RAM memory (2 or 4G), swapiness to 5, put browser cache (Chrome) in RAM then forget about the whole thing.
I rarely have issue with system crashes.
 
Given the transient nature of a system that runs purely in RAM, it may just not be worth the effort.

Yeah, there are benefits and the kernel does use swap when it is available (even on higher end devices with gobs of RAM), it may not be enough of a benefit to bother with it.

I just checked the refurb, which only has 16 GB of RAM, and the output is:

Code:
$  free -m
               total        used        free      shared  buff/cache   available
Mem:           15873        7675         995         803        7202        7067
Swap:           2047        1039        1008


However, I've never rebooted the device since the moment it was turned on after the SSD swap.

Code:
$  uptime
 16:49:09 up 25 days,  2:00,  2 users,  load average: 1.25, 1.07, 0.94

I've observed that the system uses more swap the longer it has been running, which makes sense.
I been looking on the Puppy Linux forums.
Some say you should have a Swap setup and others say it's not necessary.
I don't know although I do know I've never seem to have ever run out of ram,
 
Huh... Now that there's a nugget, when I really think about it.

Swap isn't really stored in RAM normally. So, the organization that swap provides may still be beneficial. It's still used to help manage RAM. If you read the bullet points in the OP, you'd be missing out on those benefits.

So, I'd have to guess that there's some benefit to be had.

I'd defer to the kernel. I'd try it with swap enabled (if possible). If the kernel then uses swap, I'd probably err on the side of caution and default to using swap.
I really don't know. I mean, a swapfile should work.



The only one I see that'd only impact an installed OS would be the hibernation benefit. You can't hibernate a live system, I don't believe. The rest would (almost certainly) still be benefits even if it's a live system.

Note: I'm not saying you must run swap. I just think it'd be interesting to see the results of doing so and what I would do if it did indeed work.

All sorts of people run distros without any swap at all, so it's not necessary. (Which isn't the same as being not beneficial.)
Given the transient nature of a system that runs purely in RAM, it may just not be worth the effort.

Yeah, there are benefits and the kernel does use swap when it is available (even on higher end devices with gobs of RAM), it may not be enough of a benefit to bother with it.

I just checked the refurb, which only has 16 GB of RAM, and the output is:

Code:
$  free -m
               total        used        free      shared  buff/cache   available
Mem:           15873        7675         995         803        7202        7067
Swap:           2047        1039        1008


However, I've never rebooted the device since the moment it was turned on after the SSD swap.

Code:
$  uptime
 16:49:09 up 25 days,  2:00,  2 users,  load average: 1.25, 1.07, 0.94

I've observed that the system uses more swap the longer it has been running, which makes sense.
Okay here you go.
This pertains to Puppy Linux.

Screenshot.png
 
This pertains to Puppy Linux.

I may have to poke at it some time, though the verbiage from the poster makes me wonder if they know what swap is. It's not 'extra RAM' like many think. It's much more complicated than that.

I have an early appointment tomorrow, but I'll see if I can make time to play with it tomorrow afternoon. Though, well, I'm not sure I'm qualified to opine on the matter. It's a pretty complicated subject, which is why I defer to the kernel.
 
Back in the old days when Ram was very expensive and Computers were limited to 512 MB of Ram...a Swap Partition (virtual memory) was needed...usually the rule was 1.5 x by Ram installed but those days are long gone.
m1211.gif


Move forward 25 years and things are very different...Swap is now a 2GB File because you don't need it. These days just add more Ram...problem solved.
m1212.gif


My Tower has 16GB of Ram and as you can see Swap isn't touched...
2023-10-25-16-08.png


As my Motherboard can handle more than 32GB of Ram...should I ever run out of Ram I just add more...simple.
m1230.gif
 

Members online


Top