OK, I get it.
You can improve what the kernel’s memory management does, by adding some process management in user space.
I guess in the past , process management was left to the user.
Thanks, you got me up to date on this.
Just about the only systems I ever see “thrash” into SWAP space are :
Oracle Database servers - Oracle RDBMS will start using SWAP after it starts…
Java server applets and Tomcat (java) applets running on “headless servers”… I think of Java server side as being a memory leak… i.e. Java doesn’t leak memory, it IS a memory leak - and such systems will always go into swap usage…
Of that latter (or even both) - probably badly written code perhaps? Certainly doesn’t encourage efficient resource usage (although - I’m sure most Android app developers try to optimise for limited RAM scenarios - or the Android JAVA SDK / APIs takes care of that heavy lifting - not so with hobbled together legacy apps used by big corporates - they ship already chock full of bloat and “undocumented features”…
I don’t use swap on my Ubuntu Ryzen desktop system - it has 64 GB RAM (DDR4 - but not all run at the same speed) - I have swap disabled…
I still have swap enabled on my Ubuntu Ryzen ThinkPad - it has 16 GB RAM (DDR4) - and it does use swap - because like others mentioned previously : Chromium based browser(s) with many tabs open at once …
My main RPi system (Pi4 with 8 GB RAM) running bookworm (Raspbian 12) has zram configured…
Long and lengthy and interesting (IMHO) discussion here (It’s FOSS) about zram and zswap :
Worth investigating on systems with limited RAM.
I’m using zram swap config on my 8 GB RPi4
root@frambo:~# sudo swapon -s
Filename Type Size Used Priority
/var/swap file 204796 0 -2
/dev/zram0 partition 9995384 179456
Only thing I don’t like about zram / zswap - it doesn’t have an entry in /etc/fstab… And I have to scratch my head trying to remember which file(s) it was configured in… e.g. with that Pi4 running Rasbpian Bookworm above - I setup zram so long ago - pretty much a case of “set it and forget it”…
@Rosika has successfully setup / configured zswap / zram on x86 Linux previously - I haven’t tried it other than on armhf and arm64 architectures…
Oh, yeah, going way back to the OP…
It depends pretty heavily on just how much memory you’re really talking about, but in these days of double-digit gigabyte system RAM allocations, a swap that’s even the same size as system memory is probably asking for trouble.
The conventional wisdom on swap used to be “twice your RAM”, but that was because the old, original vm systems were very stupid about how they managed swap. Way back when (we’re talking literally three or more decades ago), if you set up a swap volume for your system then the virtual address space would be swap-backed.
What that meant was, there was a block of swap space that directly corresponded to each block of the virtual address space. When the VM manager had to move virtual pages to disk, it would place them on the swap volume in exactly the same place they were mapped to in the virtual address space. So if you wanted to be able to give your 16MB system a 32MB virtual address space, you needed a 32GB 32MB!! swap volume. Having a swap drive that was the same size as RAM gained you nothing, because the only virtual memory that could ever be swapped out to disk already fit in RAM.
VM managers very quickly got smarter than that, but the whole “double your RAM” rule of thumb has been cargo-culted around ever since and WILL NOT die.
Which sucks, because with today’s systems, having swap that’s even half of your system’s RAM is playing with fire. The bigger your swap space, the more the system can over-allocate memory, and the more trouble it can get itself into. Thrashing gets MUCH worse, the more offloaded pages can be fit into swap, because it increases the likelihood that the system will have to juggle more pages than it can physically fit into RAM.
If you have a 16GB-32GB system, 8-12GB is the largest swap allocation I’d risk. On a 64GB+ system, you’re probably fine to run with no swap, as @daniel.m.tripp says, but if you must use swap keep it under 8GB. Unless you enjoy watching your system grind to a halt, while the drive activity LED turns solid <whatever-color-it-is> for minutes at a time.
My memory from the 1990’s - with Windows NT, and Solaris (and other UNIX’s) was swap should be 1.5 x physical RAM… and I used to apply that principle to Linux servers too… I seem to remember we did the same thing on Novell Netware servers too! I haven’t touched Netware for nearly 25 years - I can’t even remember probably, if, or how, you set swap on Novell Netware…
But it no longer holds true…
e.g. I’m deploying 7 x dual CPU Xeon servers with 32 cores apiece and 512 GB RAM…
If we were to attempt 1.5 x RAM swap on the main mirrored pair of SSD’s (yeah we’re using hardware RAID - which I HATE - but the vendor [of the software] insists on it) - it would be kinda ridiculous to allocate 768 GB of storage for swap space…
My 64Gb system runs fine with 400GB of swap space. It never swaps except when I run large matrix arithmetic jobs. I dont think a large swap space is any disadvantage?
It may be a waste of disk space.
Wow!
Are you counting the neutrinos travelling through the Solar System?
I am solving about 10 million simultaneous equations.
Modern algorithms for that are amazing… if it fits in ram the QR algorithm can do that in about 10 mins, depending on the number of unknowns.
Four HUNDRED gigabytes!?? Seriously?
That seems insane to me. I thought it wouldn’t be used (like, technically, not just “in practice”), but it turns out the kernel logic that governs such things is the overcommit handler, tunable with sysctl
as vm.overcommit_{memory,ratio,kbytes}
.
The standard config today is vm.overcommit_memory=0
which “compares the userspace memory request size against total memory plus swap and rejects obvious overcommits.” So your system will allow allocations up to 464 GB.
The other mode that limits commits, vm.overcommit_memory=2
, is tunable via vm.overcommit_kbytes
and vm.overcommit_ratio
. The default is vm.overcommit_ratio=50
, which adds 50% of RAM size to the swap size when determining the maximum allocation. So on your system, that would limit allocations to 432 GB.
I doubt you ever get anywhere near that, but I guess technically it does give you room. If you like thrashing. But on a system with an SSD connected directly to the PCIe bus, your swap access is probably only 5-10x slower than RAM access, instead of the 40-80x slowdown between RAM and SATA, so maybe thrashing wouldn’t be that noticeable if it happens.
(I said above that swap speed shouldn’t affect things, but it still CAN. And if you’re going to run with a swap space that’s 6.25x the size of RAM, it’d better be fast!)
I have used up to about half of it… with one R program solving lots of simultaneous equations. Yes swapping slows it down, and I only do it occasionally when there is a need to fit a model to a large set of data.
I cant use PCie mounted disks in my machine… it is sata.
Spreading swap over 2 disks on separate controllers helps.
So a newer machine with a dedicated PCie swap disk might help. That is interesting.
More ram would definitely help.
Thanks for the kernel insights.
Some Linuxes set a ulimit which sets the max memory usage of one user process. I need to set that to infinity.
Rewriting the R program in C would probably not help… because most of the critical functions in R are C or Fortran anyway.
The concept of one process using the whole machine is not new. All computers were like that once. I have worked on computer where you compiled your program, removed the primitive OS, loaded your binary into ram, pointed the program address counter to binary’s location and pressed go.
If you’re a large tab user, check out the Vivaldi browser. It has more tab management features than any browser I’m aware of. There’s also a hibernater add-on (Auto Tab Discard) that I have recently intsalled, but I’m not sure what it’s doing yet.
This info is really helpful. I just got a new Thinkbook with 32GB RAM and my swap is probably too large. It’s out for repairs. I’ll check when it comes back.
Thanks. Then I’m not totally wrong with my 16 GB desktop and 8 GB swap.