Issues with lag and hibernation when using a lot of swap

Hi,

I’ve been using Manjaro for an year now without any major problems and with this thread, I’m planning to increase my learning in the internals (I’m already using Testing branch for more than 6 months now) of OS and Linux.

Recently I’ve got a MacBook Pro from work and I’m stunned by the stability. For example, I had three windows of Firefox with 50 tabs each, Xcode, iOS Simulator, Android Studio, Android Emulator, WhatsApp, Slack and my E-Mail and Calender clients open for 30 days without any reboot and still it is fluid.

So latest info from Activity Monitor there, Firefox was using 20 GB of memory (over multiple processes), Android Studio was using 8 GB of memory, Android Emulator was using 7 GB, Xcode and iOS Simulator were using 4 GB each, Slack and WhatsApp are using 1 GB together. Still, switching between windows and apps was super fluid.

I’d love to try to get to that stability in Manjaro, and even if the goals are too high, which I agree they are, I’m sure I’d love to learn a lot more just by attempting them.

My current state:

  • 16 GB of RAM (2 x8G sticks)
  • Intel i5-11400H
  • NVIDIA RTX 3060 Mobile
  • 1x 512 GB SSD (EFI and OS partitions)
  • 1x 1 TB SSD (Home partition)
  • BTRFS filesystem
  • 20 GB Swapfile

Both the systems are always powered on and connected to a 4K Monitor (Samsung 28" LU28R550UQWXXL UHD) set at native resolution using HDMI.

Now, the issues I still face with Manjaro (not major, but again, I’d love to try fix them):

  • I cannot hibernate once I open Firefox and Slack and Android Studio for a week.
  • After three days, Alt Tabbing into any other process is taking 4-5 seconds.
  • MacOS has a separate hibernation file which is separate from Swap memory.
  • Temperatures in Manjaro are twice that of the MacBook Pro. CPU and GPU are at 50-60 C here, where MBP has 28-30C.

What are the immediate goals?

  • Find a way to have multiple Swap Files while keeping Hibernation working. I think it would be better, since having a single Swap file of 20 GB might be taxing since it has to be kept mapped in memory.
  • Find a way to dynamically increase Swap Size. This should allow for less compression and decompression times in memory pages.

Goal for future would be to try to have a separate hibernation file, so even if I fill up all the available swap, I’d be able to hibernate and resume back.

I’ve looked at zram-generator, but not sure how to get it working with BTRFS with my current setup.

Also, I don’t care about any data loss on this system, because I have made three backups already and I’m fine with bricking the SSDs too. I’m doing this, mainly for a learning experience.

Please guide me. Where should I start first?

To hibernate, you need enough swap to hold all data stored in swap, plus all data stored in RAM. Is your swap large enough?

Considering you are trying to achieve memory usage exceeding 44 GB on a 16 GB system, this is to be expected. Swap is not a magic wand that can infinitely increase your working memory. After a certain point, you start thrashing. Basically, in order for contents of swap to be used, they need to be copied to RAM. If you are using so much memory that you need to constantly move things between swap and RAM (e.g. switching between heavy programs), your computer needs to read/write that from disk, which takes a long time even on an NVME, let alone SSD. Most of the other problems you list are a result of this, most likely.

You are way over-working your PC. Either upgrade it or run fewer programs at a time, there isn’t a way to significantly speed this up from the system end.

1 Like

Yes, this is the case. This is exactly I’m looking for dynamically allocated Swap, which I suspect is what macOS is doing to support my use case.

I understand this, this is why I’m thinking of multiple smaller Swap files, so that only a few can be loaded into memory. I agree, this is more of a research task that I’d like to see, since I’ve already seen MBP do this quite well. If there is a technique, I’d love to learn it.

Are you certain your MBP doesn’t just have a larger amount of RAM? Considering the applications you run on it for your work, I would assume your employer would have purchased an upgraded model from the base 16 GB.

Thrashing is due to the size of the overall used swap, not the size of the swap files. Breaking it into smaller pieces and then using all the pieces would still mean that you are loading a ton of memory, and that would still thrash trying to reach RAM.

1 Like

Things like systemd-swap will do this fine.
But It wont magically solve your other issues.

1 Like

Yes, I’ve just checked it once again. My MBP specs are:

  • 16 GB RAM
  • 500 GB SSD

Btw, forgot to mention, both the laptops use NVME M.2 SSDs.

My understanding is that seeking to a random location in a large file will take more time, since if the Swap file is 20 GB and physical memory is 16 GB, it must be memory mapped using virtual addressing. As such, it will be multiple random seeks on the disk.

Having multiple smaller files, and be able to reserve some space in RAM for swap operations, I’m thinking that smaller files can be loaded into physical memory at once. If the file is per process, then it would be a lot more easier.

Having said that, I don’t think kernel currently supports that. I’ve been searching this for a week now without much success.

I did look into zram-generator for this purpose but the issue is mostly with hibernation with this solution. Somehow I need to get the dynamically created offset into kernel’s resume parameter before shutdown. Let me look into this once again.

As already mentioned: systemd-swap. For hibernation, you could target a swap partition. When resuming, it needs to target a device and if the device has multiple small swap files, it won’t work.

When running the same workload, can you post your RAM/swap usage from your MBP and Manjaro?

Linux doesn’t.

Virtual memory is an address space where both RAM and swap are mapped. Swap is used to offload stuff from RAM.


Your main problem, as already pointed out, is likely thrashing, as your swap seems too small. More RAM would be better but failing that more swap should help.

The usual (sensible) recommendation is 1.5x-2x the size of your RAM if you want to use hibernation :arrow_left: ignore the part about having a smaller swap than RAM, that’s for people who don’t use as much RAM/swap during normal usage.

AFAIK btrfs isn’t great for swap, have you considered a 32GB swap partition?

If you want to speed it up, stripe it across multiple SSDs (it can be done with multiple swapfiles too, 1 per drive of course). However this won’t work for hibernation as it needs to be a single partition/file, you may be able to switch between two setups one for normal usage and one for hibernation, but this would mean quite a lot of space set aside for swap.

If suspend will do then you can use multiple swap partitions/files.

This is the status on my MBP, after using it for 30 days straight. I’ll run my Manjaro laptop now for three days and will post the stats there on Monday.

No, the default install created me a 8 GB Swap partition, which wasn’t sufficient for me to even hibernate, so I disabled it and went to a Swapfile on BTRFS. I’ll probably try expanding it sometime later.

If I setup a 32 GB Swap partition and then enable systemd-swap along with it, hibernation will work right? I can keep my laptop plugged in all the time, but thinking of alternatives too if possible, since battery health will deteriorate if kept on wall power all the time.