Linux performance: almost always add swap. Part 2: ZRAM

Previously, we looked at how swap space, specifically swapping, can seriously slow down Linux performance. Then we changed the Linux kernel parameters to make better use of server memory and avoid heavy swapping. That article gave some arguments and good arguments for and against swap space.

For example, if you have more than enough memory available, wouldn’t it be better to remove or disable swap altogether? Is swap required on VMs or containers with other defined limits? If you haven’t already, you can read Part 1 of the previous article: Linux Performance: Why You Should Almost Always Add Swap Space.

Today, I wanted to take a quick look at ZRAM. Why? Well, since this was something I failed to discuss in a previous article, I was recently reminded that on low-memory systems or in some of those scenarios where you intentionally delete swap, the correct way is to How valuable can ZRAM be when configured with .

What is ZRAM?

ZRAM forms a block device in RAM where pages that would otherwise be written to swap (disk/SSD) are instead first compressed, then stored. This allows very fast I/O of swap, and at the same time, data compression provides a significant amount of memory savings.

One downside of ZRAM is that it uses some CPU for compression, but this is usually negated by the benefits derived from avoiding disk swap and the overall memory savings of compression. So take care of those things according to your use.

Why do I need swap anyway?

nmon cpu, memory and swap monitor

On Linux systems swap can be helpful even if RAM is still available. This is because the Linux kernel will move memory pages that are rarely used into swap space to ensure that even more space is made available in memory for more frequently used memory pages.

Please note, in recent kernels, the swapping algorithm has been overhauled to consider active and inactive memory for both file-backed and anonymous (non-file-backed) memory.

Swap only becomes a performance issue when the kernel is pressured to constantly move and swap memory pages in and out of memory. Disk/SSD i/o = very slow. Read more about swap and the OOM killer.

OK, so I’ve enabled swap, now what?

Next, consider adjusting your server’s cache pressure and swap propensity by following the guide below, which is from a previous article: Linux Server Needing a RAM Upgrade? Check with top, free, vmstat and sar:

vfs_cache_pressure Controls the tendency of the kernel to reclaim memory, which is used for caching directory and inode objects. (default = 100, suggest value 50 to 500)

swappiness – Represents the kernel’s preference (or avoidance) of swap space. The swapiness value can be between 0 and 100; The default value is 60. The low value causes the kernel to avoid swapping. A higher value causes the kernel to attempt to use the swap space. Using a lower value on sufficient memory has been known to improve responsiveness on many systems. ,Source: Arch Wiki,

To edit, you can add or change these lines in /etc/sysctl.conf file. for example:

If you have low memory, you can try something like this:

vm.swappiness=10
vm.vfs_cache_pressure=500

This will increase cache pressure and it may seem counterintuitive to some extent because caching is great for performance. However, doing too many frequent swaps can significantly degrade the overall performance of your server. So not having much cache in memory will help to create space and thus reduce swap/swapcache activity. Also, setting vm.swappiness to 10 or less than 1 will reduce disk swapping.

On a healthy server with lots of available memory, use the following:

vm.swappiness=10
vm.vfs_cache_pressure=50

This will reduce the cash pressure. Since caching is good for performance, we want to store the cached data in memory for a long time. Since the cache will get bigger, we still want to minimize swapping so that it doesn’t cause increased swap I/O.

Check the current values ​​using these commands:

sudo cat /proc/sys/vm/swappiness
sudo cat /proc/sys/vm/vfs_cache_pressure

To temporarily enable these settings without rebooting, use the following commands:

sudo sysctl -w vm.swappiness=10
sudo sysctl -w vm.vfs_cache_pressure=50

Why didn’t you suggest ZRAM in the first place?

The previous article focused explicitly on swaps as I saw the tendency of swaps to be removed as unnecessary. That article primarily serves to address why you should almost always add swap space.

Last month, I switched the laptop to the PineBook Pro. It’s got some optimizations and tweaks to go from Intel i7 + 16GB RAM + RAID NVMe’s and fit in the Rockchip RK3399 chip + 4GB of RAM and much slower eMMC storage. Right off the bat, memory was the biggest bottleneck.

To address this, I had to override some of the default kernel parameter values ​​and set the ZRAM. Also I had to install the great suspenderDisable composition and animations, among other tweaks. I will update that article soon.

But let’s keep this article related to Linux servers! Using ZRAM (or ZSWAP) reminded me how it can stabilize Linux server performance in low memory situations.

Adjusting kernel parameters for ZRAM instead of SWAP

On low-memory servers, after adding ZRAM, you can add the following lines to /etc/sysctl.conf Then enable in real-time with the appropriate commands.)

vm.vfs_cache_pressure=500
vm.swappiness=100
vm.dirty_background_ratio=1
vm.dirty_ratio=50

With the above settings, you will delay the inevitable (out of memory), But start preparing for it as soon as possibleInstead of leaving it until the inevitable happens all at once – swap heavily.

vm.vfs_cache_pressure=500 – Increases cache pressure, which increases the tendency of the kernel to reclaim memory used for caching directory and inode objects. You will use less memory in the long run. The downside of swapping negates the performance hit too soon.

vm.swappiness=100 – Since we use ZRAM first, increasing how aggressively the kernel will swap memory pages.

vm.dirty_background_ratio=1 – Background processes will start writing immediately when the 1% limit is reached

vm.dirty_ratio=50 , The system will not force synchronous I/O until it reaches a 50% dirty_ratio.

These four lines – when used with ZRAM – will help with performance in cases where you Will Eventually more than the installed amount of memory is needed. Knowing that you will swap, that ZRAM makes swapping less expensive and compresses the same data 2x to 3x smaller, you should start this swap exchange sooner.

Cache pressure helps because we’re actually telling the kernel: “Hey look man, I don’t have any spare memory to use for cache, so please remove it as soon as possible and store only the most used ones.”

Even with less caching, if we use up most of the installed memory over time, the kernel will very quickly start opportunistic swapping in ZRAM so that the CPU (used for compression) and swap I/O are not delayed all at once. Ho.

Then, as a last resort, you can optionally have a little swap space. which looks something like this:

$ sudo cat /proc/swaps
Filename   Type      Size    Used   Priority
/swapfile  file      511996  0      -1
/dev/zram0 partition 2097144 399320  5

Or, for servers with low cache pressure, you can encourage even less than the default cache pressure when using ZRAM (keep cached data longer):

vm.vfs_cache_pressure=50 
vm.swappiness=10 
vm.dirty_background_ratio=1 #increase slowly, up to 10%, if CPU gets too busy with compression. 
vm.dirty_ratio=20 #around 20 to 50 percent is fine.

Can I remove SWAP/ZRAM completely?

As discussed, swap also provides some benefits for overall system stability and performance. The kernel was designed to work with swap. With that said, your specific setup or requirements may work best without swap or ZRAM. red hat puts it well,

“Systems without swap make sense and are supported by Red Hat – just make sure the behavior of such systems under memory pressure is exactly what you want. In most environments, a bit of swap makes sense. Without swap, the system would call OOM when the memory ran out. [Make sure to] …prefer which processes finish first in configuring oom_adj_score.”

conclusion

Using ZRAM, especially in low-memory setups, is very useful in allowing your Linux server to use up more of your installed RAM.

For example, 1GB of ZRAM is roughly equivalent to 2 to 3GB after compression. In addition, we encouraged the kernel to swap in ZRAM more frequently in our tweaks, thus freeing up more usable RAM with or without swap. The potential improvements in stability and performance make it worth a try. let me know how it goes!

Published: February 5, 2020
Last Updated: October 18, 2021

Leave a Comment