Does ZRAM or ZSWAP makes sense in WSL2? #10862
-
|
Windows has its own RAM compression system, and WSL2 ultimately runs inside Windows. Does the Windows RAM manager compresses the Hyper-V RAM as well? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
|
Anyone? |
Beta Was this translation helpful? Give feedback.
-
|
ZRAM works very well inside WSL2. In the meantime, you could use the zRAM enabled wsl2 kernel https://github.com/Locietta/xanmod-kernel-WSL2?tab=readme-ov-file#usage |
Beta Was this translation helpful? Give feedback.
-
|
@mangkoran Alright as promised, guide to getting zswap turned on (zram involves per-distro configuration (need to set up startup tasks!) and might cause inversion BS with WSL2's disk swap file)
Also useful links are:
Why high swappiness?Swappiness determines whether the kernel prioritizes emptying the file pages (by writing to disk, usually) or emptying the anonymous pages (by moving them to swap) when memory is short. As a result the ideal swappiness is (100 × (IO cost of emptying file page) / (IO cost of emptying anonymous page)). The IO cost of emptying a file page comes two things:
The IO cost of emptying an anonymous page also comes from two things:
The kernel default swappiness is 60, which is very reasonable assuming the same IO device is used for file and swap. After all it's two potential costs against one definitive cost plus one potential cost. However, zswap and zram lowers the effective cost of reading and writing to swap. Zswap does it by acting as a LRU cache in front of swap. Zram does it by acting as the swap. So our cost for emptying an anonymous page becomes:
This creates a situation where triggering the swap system may be cheaper than file IO even without considering filesystem overheads. It is totally possible for a page to be instead put somewhere in the RAM, which for most intents and purposes have near zero IO cost compared to disk. On top of that we have filesystem and other overheads for file IO. In the case of WSL, well we all know about the overhead of accessing Windows drives via "drvfs". So what does decreased swap IO cost and increased file IO cost mean? That's right, higher swappiness is only logical. Example performance valuesWorkload: VS Code and Haskell Language Server # ./zswap_stats
Zswap Statistics:
-----------------
Stored Pages: 502402
Pool Total Size: 533MB
Uncompressed RAM: 2.0GB
RAM Saved: 1.4GB
# free -h
total used free shared buff/cache available
Mem: 3.8Gi 3.6Gi 171Mi 1.1Mi 271Mi 226Mi
Swap: 4.0Gi 2.6Gi 1.4Gi
# grep -r . /sys/kernel/debug/zswap/
/sys/kernel/debug/zswap/stored_pages:502170
/sys/kernel/debug/zswap/pool_total_size:558354432
/sys/kernel/debug/zswap/written_back_pages:154615
/sys/kernel/debug/zswap/reject_compress_poor:0
/sys/kernel/debug/zswap/reject_compress_fail:505
/sys/kernel/debug/zswap/reject_kmemcache_fail:0
/sys/kernel/debug/zswap/reject_alloc_fail:0
/sys/kernel/debug/zswap/reject_reclaim_fail:143
/sys/kernel/debug/zswap/pool_limit_hit:0
# grep -r . /sys/module/zswap/parameters/
/sys/module/zswap/parameters/enabled:Y
/sys/module/zswap/parameters/shrinker_enabled:Y
/sys/module/zswap/parameters/max_pool_percent:20
/sys/module/zswap/parameters/compressor:zstd
/sys/module/zswap/parameters/zpool:zsmalloc |
Beta Was this translation helpful? Give feedback.
-
|
There is a new version of the script that compiles the official Microsoft Kernel adding zswap. It was updated for the kernel 6.x case with external vhdx modules disk. |
Beta Was this translation helpful? Give feedback.




ZRAM works very well inside WSL2. In the meantime, you could use the zRAM enabled wsl2 kernel https://github.com/Locietta/xanmod-kernel-WSL2?tab=readme-ov-file#usage