Score:1

Automatically manage swap / memory

bd flag

When filling up all memory and swap my experience the most recent years has been that:

  • Windows and macOS go "oh, let's fix that for you" and then presumably increase the swap size. Opening e.g. 30 YouTube tabs slows things down for a while, but I can generally still use the cursor if I want to abort the process, and after that the rest of the system (not necessarily the offending app) is pretty much as responsive as always.

  • Ubuntu (21.04) goes "sorry, you can't interact with the computer for the next ten to thirty minutes". On the same hardware and the same browser, I can open 30 YouTube tabs and watch the whole system lock up. All memory and swap is used up, and the cursor is not just slow but locked in place. I can't meaningfully interact with the rest of the system.

The difference seems to be that Ubuntu never resolves the situation, keeps its swap size constant and consequently keeps locking up every five seconds, forever. Until, that is, catastrophic failure occurs and Firefox disappears without a trace — something that virtually never happens in other OSes. The most crucial aspect is that I'm unable to interact with the rest of the system.

I don't know, but I get the impression this would not be an issue if the swap size were increased based on demand. How is that accomplished? Is there better memory management to be had somehow?


Test update

After (not so quickly) doing the quick test suggested in the comments where I doubled the size of the swapfile I can conclude that it makes all the difference in the world. The system remained responsive throughout during the same load. I can only conclude that an adaptive swap size would alleviate the described problem.

$ free -h
              total        used        free      shared  buff/cache   available
Mem:          3.7Gi       3.1Gi       129Mi       374Mi       566Mi       105Mi
Swap:         4.0Gi       3.0Gi       988Mi

$ sysctl vm.swappiness
vm.swappiness = 60

$ swapon -s
Filename                Type        Size    Used    Priority
/swapfile_4G.img                        file        4194300 3180664 20

A note on hardware

One of the selling points of Ubuntu (among others) is to get continued support and increase the life-span of older hardware, which includes machines with less than average total memory. Not absurdly little memory, as demonstrated by the fact that other OSes can make do just fine, just less than average. Many of these machines are non-upgradable.

4 GB RAM works fine for most use cases. In Ubuntu, in Windows, in macOS. What only seems to work fine in Windows and macOS, however, is when you use up all allocated swap space.

In short, the same instant that the solution to making Ubuntu run decently becomes "buy new hardware" the whole point of running it becomes moot for all these machines. Upgrading the hardware is out of scope for this question.

The requirements for the standard edition of Ubuntu is 4 GB of memory.

Zanna avatar
kr flag
Comments have been [moved to chat](https://chat.stackexchange.com/rooms/126367/discussion-on-question-by-andreas-automatically-manage-swap-memory) (hoping to return focus to the question of it/how "adaptive swap" effects can be achieved on Ubuntu)
Score:1
in flag

The Linux kernel is... iffy in terms of swap support. Swap is a second-class citizen, from the perspective of many developers. What I mean by that is that the algorithm used to eject and fetch pages is fairly simple, and can break down when there are a lot of frequently-used pages.

A decent solution would probably be to use zswap. What this does is set a chunk of memory to be compressed memory, to which pages are ejected. To do this, edit the kernel boot parameters to add zswap.enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=20 zswap.zpool=z3fold. The easiest way to do this on a Ubuntu system is to add it to the /etc/default/grub file, as part of the GRUB_CMDLINE_LINUX_DEFAULT key. This will effectivly increase the amount of data that can be stored in RAM, reducing disk IO and preventing lockups.

Lastly, you ask about having more swap space dynamically created. Linux is fully capable of adding and removing swapfiles at will, which means that a script could check swap availability, and then add swapfiles if needed. However, there isn't much point to this if your using the full swap space very often.

To increase swap space (without repartitioning your disk), you can use a swapfile. To do this, run sudo dd if=/dev/zero of=/swapfile count=1M bs=1024 status=progress, which copies 1 million blocks of 1024 bytes (ie, 1 gigabyte) into a file. Then, run sudo chmod 0600 /swapfile and sudo mkswap /swapfile to prepare the file for use as swap. Add it to /etc/fstab to have it always available, by appending the following line to that file: /swapfile swap swap defaults 0 0. To finish the process, call sudo swapon -a, which activates the swapfile.

Source: I myself use swapfiles and zswap, and referenced my bash_histroy and the Arch wiki for the commands.

bd flag
Thank you for a relevant answer. I've just tried out dynamic swap space allocation using the readily available `swapspace` package, as suggested in a comment, and thought I'd now take a look at your memory compression suggestion. The system in question already has a process called `zswap-shrink` running though -- not sure if that indicates that zswap is enabled in any way.
bd flag
One question: is there any way I can confirm that memory compression works after enabling it? It at least seems completely transparent as far as e.g. the system monitor goes.
Calum McConnell avatar
in flag
Based on [this answer](https://unix.stackexchange.com/a/412760/450166), the easiest way is “sudo grep -R /sys/kernel/debug/zswap”. Grep is used because the files in the /debug/ tree aren’t user visible, so shell expansion won’t work. It also adds file labels next to each of the numbers, making the thing actually readable.
bd flag
Thank you. Did you mean to omit the regex (`.`)? Without it weird stuff happens :) With it I get the statistics, however everything is zero even at 93% RAM and 40% swap usage. Unsure how to interpret that
Score:0
bd flag

As was mentioned in a comment by @Terrance Swapspace is a solution to this problem.

apt install swapspace

Now all you need to stay in control of your system is some free storage. Tested and works.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.