Linux Performance: Almost Always Add Swap Space

I have this set up on my laptop and the main reason is that it is only 6gb. I can probably do without it but I like to maintain a certain speed when using it. It is an older laptop and I plan to get a new one soon anyway.

The article addresses this. Even on a healthy system its useful to encourage opportunistic swapping. The longer your uptime will be, the more important opportunistic swapping becomes. On a system you shut down every day, not so much.

Generally unless the ram we install or configure comes free of cost, we should only install just a tad more than the system needs to run. Anything more is a waste of :moneybag:.

Overabundance of RAM is about the the only time opportunistic swap isn’t useful. For example if one has 64GB of RAM on a server or desktop and after days or weeks of uptime only half of it is used, then its literally wasted hardware and thus no need for opportunistic swapping to move the LFU or rather LRU data to the disk in order to ensure ram is being used just for the most active stuff.

Swap is kinda like those couple of boxes we have sent to storage because we never open or use the items inside. But if the # of boxes continues to grown, then it becomes obvious we may need a bigger house.

But living without swap is like living without storage and just stepping over very old and seldomly accessed data, because there’s no storage unit to put them in.

Or, a family of 3 living in a 8 bedroom house, yup, definitely may not need a storage room. At least with a mansion we can make use of all of it if we choose to. But with RAM if you can only make use 3GB and you have 8GB it just sits there, never used.

2 Likes

I have 16GB on my laptop,but sometimes i use swap on my systems depending on what my use case will be,might however not be needed,but what if i suddenly need it someday…so better safe than sorry maybe?:slight_smile:

Loved this read (and Part 2).

It was very interesting reading about ZRAM–particularly (like you mentioned) with the memory-saving benefits it comes with–and a good refresh on how the Linux kernel manages memory pages to optimize memory performance and availability.

The advantage of allowing enough response time for admins is huge (especially with a monitoring/alert system that may or may not be in place), conceptually at least.

Will be taking note of the memory management commands, thanks again @hydn :+1:

2 Likes

Thanks for taking a moment to leave this feedback. It makes me realize what’s been lacking for the past ten years; being able to mingle, share, learn and understand the different perspectives of peers.

I’ll post up part 2 to the Blog articles forum.

This week I gutted the blog menu to focus more on allowing readers to find the articles which interest them. You can view that here: Browse Articles - Linux Systems Analyst | Hayden James

2 Likes

This may seem overkill but, I have a 32 core 256GB RAM server that I will run Debian 11 as a KVM host for virtual machines. While setting up the VM’s will be easier after this read, I would like your opinion on setting up swap for the VM host. Bear in mind that these cores and RAM will be allocated to the VM’s with at least one VM getting 128GB RAM for a massive SQL server.

1 Like

I have a 12 core, 64GB desktop dual boot Ubuntu 22.04 for work and Win 11 for games, photo editing etc (hence the need for more RAM)

When I recently rebuilt it I decided to let the default LVM disk partitioner do what it felt like to see if it believed if needed swap. It did and allocated 976 MiB for swap. So far I have not seen it used but I will keep an eye on it and update this if I find there is a scenario that makes use of it.

1 Like

64GB sounds like a TON of ram for what you describe. I would love to see a screenshot of your top command stats. If that was not the cause, you would have already seen a very small amount of opportunistic swap when the system is idle.

You may not see any swap at all if there is far more memory available than what can be cached and used for applications.

Yeah agreed it’s total overkill when running under Linux but it has definetly smoothed things out when running Da Vinci Resolve under Windows.

Here’s a screenshot of top as requested.

That’s with 4 x VSCode windows, Thunderbird, Vivaldi and several terminals.

1 Like

Looks good. Interesting that 30GB sitting free and unused.

You can probably tweak your cache pressure. Default is usually:

vm.vfs_cache_pressure=100

Change to:

vm.vfs_cache_pressure=10

You probably still won’t touch swap. haha But with so much memory just sitting there, you might as well tell the kernel not to reclaim memory as often to use more of it. Over time, you will use more of it. Won’t make a noticeable difference but that’s probably the OCD part of me, just knowing it’s setup for use. :man_facepalming:

Thanks for sharing!

LOL, yeah I totally get it.

TBH I haven’t really spent any time tweaking 22.04 from a performance standpoint. I only switched over to it a couple of weeks back so I’m just letting it run as it came out of the box, for the most part, to see what it does and doesn’t do by default.

Then I’ll spend some time seeing what I can improve.

1 Like

Hello, signed up for your forum just because of this line. Had a debate with an experienced sysadmin and he pointed this out to me, I was wondering what your thoughts on it are:

Welcome to the community!

It depends on a lot of factors, often changing, Kernel versions and even distro-specific variables. The original article was written back in 2017.

2018 there PSI was added: Getting Started with PSI · PSI + PSI - Pressure Stall Information — The Linux Kernel documentation

More recently, some distros are released with ZRAM enabled by default or preconfigured for easy enabling.

But in general, the Linux kernel is designed to swap as opportunistically as possible. The kernel’s swap logic is generally to swap out pages opportunistically, which generally is better for system performance. As such, I think opportunistic swapping will always be part of the design of swapping, no matter the changes over time.

Hey thanks for the quick reply.

The debate started started because of a mariadb server issue: The mariadb service was having issues by locking up, and then one time the entire server itself locked up. When I discovered he was running without swap on a 4-core 16 GB configuration my head immedatiely suspected it was because of zero swap. When I learned more, it turns out the entirety of the database is ~2 GB in size – and he now has it running on a 16-core 64 GB configuration. I lost my bearings, because it really and truly shouldn’t be because lack of swap space on this type of excess memory, 16 or 64. In the back of my head I still think there are age-old base assumptions somewhere in the code that there’s always swap available, but I suspect if/when he creates swap, ZRAM or otherwise, the kernel will never use it. It’ll be interesting to see what happens.

1 Like

Yes, it’s unlikely that locking up would be due to no-swap. Despite the benefits of swap, the opposite would be more likely, which then would have allowed you then a chance to check things out and resolve. But there’s no swap space, so like I said, probably not related to that.

WITHOUT swap, the OOM killer would have nuked MariaDB (if using most memory) without any warning. [But you said there’s excess memory.]

WITH swap, you generally have a heads up by way of degrading performance when swap is no longer opportunistic :slightly_smiling_face: and starts to take place often enough that it hurts performance at important times.

What about VM’s? Does any/all of the above replies apply 1:1 to a Virtual Machine?

1 Like

It’s never going to be an absolute “yes” hence the “almost always” in the title. But you will have to gauge on a case-by-case basis. But also know that most times swap space should indeed be enabled. Especially now with zram being enable or installed by default on some distros.