Use zram and zswap together

Is it possible to use zram and zswap on the same system by increasing the zram priority? I have read several questions and answers about using both individually, but only one together. Is it still ill advised?

Additionally, which would be best for an HDD, SSD, USB, and SD card?

Why? What benefits are you looking for, and what’s your usecase?

Basically, I have a ZFS root and my builds have a bad habit of not hitting cache and committing suicide. :sweat_smile: Apparently swap on zvols aren’t a good idea, and I was wondering whether 24 GB RAM would be enough, and whether I could set up swap anyway, just in case.

I get by with 16GB of RAM + zram, and while sometimes I need to set max-jobs to 1, that’s normally enough for me to manage building even the worst-case builds like browser engines.

What memoryMax value would you recommend, if any?

It’s not like I’ve fine-tuned this, but I use 150% of RAM size. Not sure why the default is quite as low as it is.

I believe that’s memoryPercent, not memoryMax…?

Yes, but they’re 2 different ways to specify the same thing:

Ah; got it. But wouldn’t that mean that the setting is attempting to store more data in RAM than can fit, considering zram is a part of the RAM that’s set aside…?

The size limit is the pre-compression size, not post-compression. So not generally, no.

Yes, but if I had 24 GB RAM, 150% of that would be 36 GB, so wouldn’t the setting be trying to store 36 GB of data in a part of 24 GB of RAM? Sorry, zram’s a little confusing to me, even with everything I’ve read.

Yes, but 36GB of data, once compressed, would likely actually fit into 24GB with room to spare.

Okay, well here’s the problem: if a build is using too much RAM, you will invariably spend too many CPU cycles on I/O thrashing. zram not being on disk avoids some overhead, but (de/re)compression still does take CPU cycles and IME did not really prevent OOM for large builds :person_shrugging:

What I’ve found far more effective is to limit cores and max-jobs.

Or buy more RAM.

Hmm… On average, would you happen to know how much data in RAM is compressible…?

Currently, for me:

$ zramctl
NAME       ALGORITHM DISKSIZE  DATA  COMPR  TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd         23.3G  4.1G 967.7M 994.7M      12 [SWAP]

So 4:1 in this instance.

I’ve seen ratios as high as 10:1 before, too, sometimes.

Hmm… Would a 13th Gen Intel Core i3 with 24 GB RAM be alright, then? Would I be able to build, for example, Firefox from source using all cores, or would I still have to limit them?

Fair enough! I’ll try it! It’s easy enough to change, right?

Regardless, in general, would using both zram and zswap work, or would they interfere with one another?

Browser builds use absurd amounts of RAM, it’s not really a CPU issue here. Reduce the number of cores used by nix in that case - this can reduce the amount that needs to be juggled in RAM. While obviously that will make the build take noticeably longer, you might be able to actually complete the build.

FWIW though I went down this road (building firefox and OOMing) for several months before increasing my RAM from 32 to 64 gigs, as I didn’t really enjoy the slower builds and ultimately under-utilized CPU.

1 Like

Oh dear; RAM is expensive for a university student. :sweat_smile: I’ll try to hit the cache more often, then, but I’ve always wanted to use the unstable channel…